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
python manage.py createsuperuserUsername: 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
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
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.
Name Fee Duration months Is online Graphic Designing 6000 3 False SEO 5000 2 True Web Development 8000 6 False
ModelAdmin Options
| Option | What it does |
|---|---|
list_display | Columns shown in the list |
list_filter | Filter boxes in the right sidebar |
search_fields | Adds a search box over these fields |
ordering | Default sort order |
list_editable | Lets you edit these columns directly in the list |
readonly_fields | Fields shown but not editable |
date_hierarchy | Date links at the top for quick browsing |
Show Related Rows Inline
Edit students directly on their course's page with an inline:
Example
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
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?
