Topic 09 / 12

The Django Admin Interface

~12 min read  //  Django Part 1 Series  //  Coding India

1. What is the Django Admin?

The Django admin is one of the framework’s most famous features. With almost zero code, it generates a complete, professional-grade web interface for managing your application’s data. It is not meant for end users — it is a powerful internal tool for developers, content managers, and data administrators.

2. Creating a Superuser

The admin is protected by authentication. You need to create a superuser account — an admin user with all permissions. Run this command and follow the prompts:

python manage.py createsuperuser
Username: admin
Email address: [email protected]
Password: ************
Password (again): ************
Superuser created successfully.

3. Registering Your Model

By default, the admin does not know about your custom models. Open blog/admin.py and register the Post model:

# blog/admin.py
from django.contrib import admin
from .models import Post

# Basic registration — one line is all you need to see the model in admin:
admin.site.register(Post)

Now start the server, go to http://127.0.0.1:8000/admin/, and log in with your superuser credentials. You will see a “Blog” section with “Posts” listed. You can create, edit, and delete posts through the GUI without writing a single line of frontend code.

4. Customising the Admin with ModelAdmin

The default admin view is functional but plain. Customize it using a ModelAdmin class:

# blog/admin.py
from django.contrib import admin
from .models import Post

@admin.register(Post)  # This decorator replaces admin.site.register(Post)
class PostAdmin(admin.ModelAdmin):
    # Columns to display in the list view
    list_display = ['title', 'author', 'published', 'created_at']

    # Filters shown in the right sidebar
    list_filter = ['published', 'created_at', 'author']

    # Makes the list searchable by these fields
    search_fields = ['title', 'body']

    # Pre-populate the slug field based on the title as you type
    prepopulated_fields = {'slug': ('title',)}

    # Add date-based navigation at the top
    date_hierarchy = 'created_at'

    # Default ordering in the list view
    ordering = ['-created_at']

With this configuration, your admin list page now shows a sortable, filterable, searchable table of posts — exactly like a professional content management system, built in minutes.

5. Admin Actions

You can add bulk actions that apply to multiple selected items. Here is how to add a “Publish selected posts” action:

# blog/admin.py
from django.contrib import admin
from .models import Post

def publish_posts(modeladmin, request, queryset):
    queryset.update(published=True)

publish_posts.short_description = "Mark selected posts as published"

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ['title', 'author', 'published', 'created_at']
    actions = [publish_posts]  # Add the custom action here