Admin Site Setup

Difficulty: Intermediate

Django's admin interface is one of its most celebrated features. It automatically generates a web-based administration interface for your models, giving you a fully functional CRUD (Create, Read, Update, Delete) interface with minimal configuration. The admin is designed for site administrators and content managers, not for end users.

The admin site is provided by django.contrib.admin, which is included in INSTALLED_APPS by default. The admin URL is configured in your root urls.py with path('admin/', admin.site.urls). To access the admin, you need a superuser account, created with 'python manage.py createsuperuser'.

To make a model appear in the admin, you register it in the app's admin.py file. The simplest approach is admin.site.register(Model), which creates a basic admin interface with default settings. For customization, you create a ModelAdmin class and register it with the model using the @admin.register decorator or admin.site.register(Model, ModelAdminClass).

The admin automatically discovers admin.py files in all installed apps through the autodiscover mechanism. When Django starts, it imports admin.py from each app in INSTALLED_APPS, executing the registration code. This is why you define your admin configurations in admin.py - Django knows to look there.

The admin dashboard shows all registered models grouped by app. Each model gets list, add, change, and delete views automatically. The list view shows all objects in a table. The add/change views display a form generated from the model's fields. The admin respects model field types, choices, and validators to generate appropriate form widgets.

You can customize the admin site itself by modifying admin.site attributes: admin.site.site_header changes the header text, admin.site.site_title changes the browser title, admin.site.index_title changes the dashboard title, and admin.site.site_url changes the 'View Site' link.

Code examples

Basic Model Registration

# blog/admin.py
from django.contrib import admin
from .models import Article, Category, Tag

# Simplest registration
admin.site.register(Category)
admin.site.register(Tag)

# Using decorator (recommended)
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    pass  # Default admin interface

# Or without decorator:
# admin.site.register(Article, ArticleAdmin)

admin.site.register() makes a model available in the admin. The @admin.register decorator is the preferred syntax. A ModelAdmin class with no customization gives you the default interface.

Creating a Superuser

# Create a superuser for admin access
# $ python manage.py createsuperuser
# Username: admin
# Email: admin@example.com
# Password: 
# Password (again): 
# Superuser created successfully.

# Or create programmatically (e.g., in a data migration)
from django.contrib.auth.models import User

User.objects.create_superuser(
    username='admin',
    email='admin@example.com',
    password='securepassword123',
)

Superusers have all permissions and can access the admin site. Create one with createsuperuser management command or programmatically using create_superuser(). Never commit passwords to version control.

Customizing the Admin Site

# project/admin.py or any app's admin.py
from django.contrib import admin

admin.site.site_header = 'My Project Admin'
admin.site.site_title = 'My Project Admin Portal'
admin.site.index_title = 'Welcome to the Admin Dashboard'
admin.site.site_url = 'https://myproject.com'

# You can also set these in urls.py:
# from django.contrib import admin
# admin.site.site_header = 'My Project Admin'
# urlpatterns = [
#     path('admin/', admin.site.urls),
# ]

Customize the admin site appearance with admin.site attributes. site_header appears at the top of every page, site_title appears in the browser tab, index_title is the dashboard heading.

Admin URL Configuration

# project/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    # Default admin URL
    path('admin/', admin.site.urls),

    # Custom admin URL for security through obscurity
    # path('secret-dashboard/', admin.site.urls),

    # App URLs
    path('blog/', include('blog.urls')),
]

# Access the admin at: http://127.0.0.1:8000/admin/
# Login with superuser credentials
# The admin provides:
# - Dashboard with all registered models
# - List view with search, filter, and pagination
# - Add/Edit forms with validation
# - Delete confirmation
# - Action dropdowns for bulk operations
# - History/audit log for changes

The admin is mounted at whatever URL you configure. Some teams change the admin URL path for basic security hardening. The admin provides a complete CRUD interface with built-in features like search, filtering, and bulk actions.

Key points

Concepts covered

admin.site, register, superuser, createsuperuser, Admin URL