Django Tutorial menu

Django Admin

Django gives you a complete admin panel for free. You can add, edit and delete data without writing any pages.

Create an Admin User

Example

bash
python manage.py createsuperuser
Output
Username: admin
Email address: admin@example.com
Password:
Password (again):
Superuser created successfully.

The password is hidden while you type. Now run the server and open http://127.0.0.1:8000/admin/. Log in with the username and password you chose.

Use a strong password, even on your own computer. Many people forget to change it before going live.

Register Your Models

At first, the admin only shows Users and Groups. To manage your own models, register them in courses/admin.py:

Example

python
from django.contrib import admin
from .models import Course, Student

admin.site.register(Course)
admin.site.register(Student)

Refresh the admin and a Courses section appears with Courses and Students. Each row shows the text from the model's __str__ method.

Customise the List Page

A ModelAdmin class controls how a model looks in the admin:

Example

python
from django.contrib import admin
from .models import Course, Student

@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
    list_display = ["name", "fee", "duration_months", "is_online"]
    list_filter = ["is_online", "duration_months"]
    search_fields = ["name"]
    ordering = ["name"]

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    list_display = ["name", "email", "course", "joined"]
    list_filter = ["course"]
    search_fields = ["name", "email"]
    date_hierarchy = "joined"

The @admin.register(...) decorator does the same job as admin.site.register(...). Use one or the other, not both.

Output
Name               Fee    Duration months   Is online
Graphic Designing  6000   3                 False
SEO                5000   2                 True
Web Development    8000   6                 False

ModelAdmin Options

OptionWhat it does
list_displayColumns shown in the list
list_filterFilter boxes in the right sidebar
search_fieldsAdds a search box over these fields
orderingDefault sort order
list_editableLets you edit these columns directly in the list
readonly_fieldsFields shown but not editable
date_hierarchyDate links at the top for quick browsing

Edit students directly on their course's page with an inline:

Example

python
class StudentInline(admin.TabularInline):
    model = Student
    extra = 1

@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
    list_display = ["name", "fee"]
    inlines = [StudentInline]

extra = 1 shows one empty row for adding a new student. (Use this version of CourseAdmin instead of the earlier one.)

Change the Admin Titles

Example

python
admin.site.site_header = "Workownix Admin"
admin.site.site_title = "Workownix"
admin.site.index_title = "Manage courses and students"

Who Can Use the Admin?

  • Superusers can do everything
  • Staff users can log in, but only see what their permissions allow
  • Normal users cannot open the admin at all

You manage users and permissions from the admin itself, under Users and Groups.

Exercise

Exercise

Which ModelAdmin option chooses the columns shown in the admin list page?

Learn Django with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp