Chapter 8 · 10 min read
The Admin Site
The Admin Site
Django includes a ready-made back office that reads your models and gives you tables and forms to add, edit and delete data. You get it almost for free, and it is invaluable while you build the rest of your site.
What you will learn
- How to create a superuser.
- How to register a model in the admin.
- How to customise the list page with
list_display,list_filterandsearch_fields. - What the admin is for, and what it is not for.
What the admin is
The admin site is provided by django.contrib.admin, which is already in INSTALLED_APPS. It is mounted in config/urls.py at admin/. It is meant for you and your staff. Visitors to your public site should use pages you build with views and templates. In Django 6.1 the admin's change forms place each label above its field, a small accessibility improvement you will notice immediately.
Step by step
1. Create a superuser
A superuser is an account with every permission.
python manage.py createsuperuser
Django asks for a username, an optional email address and a password (twice). The password is invisible while you type: that is normal. Django also refuses very weak passwords, using the validators listed in AUTH_PASSWORD_VALIDATORS in settings.
2. Register the model
Replace blog/admin.py:
from django.contrib import admin
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ["title", "author", "published", "created"]
list_filter = ["published", "created"]
search_fields = ["title", "body"]
Walkthrough:
@admin.register(Post)tells the admin to managePostusing the class below. The shorter formadmin.site.register(Post)also works but gives you no customisation.list_displaychooses the columns on the list page. Without it you would see only the result of__str__.list_filteradds a filter sidebar.search_fieldsadds a search box that searches the listed fields.
3. Use it
Start the server with python manage.py runserver and open http://127.0.0.1:8000/admin/. Log in with your superuser. You will see sections for Groups, Users and Posts.
- Click Add next to Posts.
- Choose yourself as the author, type a title and body, and save.
- Add a second post and leave Published unticked.
- On the list page try the filter sidebar and the search box.
More options you will meet
| Option | What it does |
|---|---|
ordering | Default sort order of the list |
date_hierarchy | Drill-down navigation by date |
readonly_fields | Fields shown but not editable |
list_editable | Edit fields directly in the list |
prepopulated_fields | Auto-fill a slug from another field |
actions | Bulk actions on selected rows |
Django 6.1 added a location argument to the action() decorator so an action can be offered on the change form as well as on the list page.
Admin permissions
Staff users who are not superusers see only what you grant. In the admin, open a user, tick Staff status, and choose permissions or groups. Use groups for roles such as "Editors".
Check it works
- You can log in at
/admin/with your superuser. - A Posts section is visible, and you saved two posts.
- The list page shows title, author, published and created columns, plus the filter sidebar and search box.
Exercises with solutions
Exercise 1: richer list
Show newest posts first, add a date drill-down, and make published editable in the list.
Solution.
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ["title", "author", "published", "created"]
list_editable = ["published"]
list_filter = ["published", "created"]
search_fields = ["title", "body"]
date_hierarchy = "created"
ordering = ["-created"]
Note that every field in list_editable must also be in list_display, and the first column cannot be editable.
Exercise 2: a second user
Create an ordinary user in the admin and a post that belongs to them.
Solution. Open Users, click Add user, enter a username and password, and save. Then add a post and choose that user as the author. On the list page, use the search box to find the post. Later, in the authentication chapter, you will use this user to check that nobody can edit another person's post.
Exercise 3: a bulk action
Add an action that publishes the selected posts.
Solution.
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ["title", "author", "published", "created"]
actions = ["make_published"]
@admin.action(description="Mark selected posts as published")
def make_published(self, request, queryset):
queryset.update(published=True)
On the list page, tick posts, choose the action from the drop-down and press Go.
Common mistakes
- Forgetting
@admin.registeroradmin.site.register. The model never shows up. - Migrations not applied. The admin crashes with
no such table. - Treating the admin as the public site. Build real pages with views and templates.
- Forgetting the superuser password. Reset it with
python manage.py changepassword USERNAME. - Putting a non-editable field in
list_editable. Django's system check reports it.
Key takeaways
createsuperusermakes your first account.- Register models in
admin.pyand customise lists withModelAdminoptions. - The admin is for staff, not for visitors.
- You will use it constantly while developing.
Official links
- Admin site reference: https://docs.djangoproject.com/en/6.1/ref/contrib/admin/
- Tutorial part 7 (customising the admin): https://docs.djangoproject.com/en/6.1/intro/tutorial07/
