Django Admin Site
Django's admin site is an automatically generated, production-ready interface for managing your application's data models.
What the Admin Site Gives You
Every Django project includes django.contrib.admin, an app that inspects your registered models and builds a full create, read, update, and delete interface for them automatically. You get list pages, detail forms, validation, search, and filtering without writing a single view or template. It is designed for trusted staff users such as editors and support teams, not for public-facing pages.
Creating a Superuser
Logging in to the admin requires an account with staff and superuser privileges. Run this command after your migrations have created the auth tables, then follow the interactive prompts for a username, email, and password.
Example
python manage.py createsuperuser
Username: admin
Email address: admin@example.com
Password:
Password (again):
Superuser created successfully.Registering and Customizing Models
A model is invisible to the admin until it is registered in the app's admin.py file. Registering it bare with admin.site.register(Model) gives a working but generic interface. Registering it with a ModelAdmin subclass, either by passing the class as a second argument or using the @admin.register decorator, lets you control which columns appear, which fields are searchable, and how records are filtered and ordered.
Example
# blog/models.py
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
published = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
# blog/admin.py
from django.contrib import admin
from .models import Article
admin.site.register(Article)Example
# blog/admin.py
from django.contrib import admin
from .models import Article
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title', 'published', 'created_at')
list_filter = ('published',)
search_fields = ('title', 'body')
ordering = ('-created_at',)- list_display — choose which model fields appear as columns in the change list
- list_filter — add a sidebar of filters for quickly narrowing the list
- search_fields — enable a search box across the listed fields
- ordering — set the default sort order for the change list
- readonly_fields — display a field's value without allowing it to be edited
- prepopulated_fields — auto-fill one field, like a slug, from another as the user types
Exercise: Django Admin
Which command creates an admin superuser account?