Django Admin — Administer Articles in the Backend

In this chapter, you will learn how to configure Django Admin to have a content management backend without writing any frontend code.


What is Admin?

Django AdminIt is one of Django's most powerful built-in features: you just register models, and it automatically generates a complete admin management system.

The admin provides article CRUD, search, filtering, pagination, and other functions, without needing to write any CRUD code yourself.


Creating a superuser

First, you need to create an administrator account to log into the admin:

(venv) $ python manage.py createsuperuser
用户名:admin
电子邮件地址:[email protected]
Password:        # 输入密码(不会回显)
Password (again): # 重复密码
Superuser created successfully.

After starting the server, visit http://127.0.0.1:8000/admin/ and log in with the account you just created.

(venv) $ python manage.py runserver

Registering models with Admin

Openblog/admin.py, register the Post and Category models.

Example

# File path: blog/admin.py
from django.contrib import admin
from .models import Category, Post

# Registering the Category Model: The Easiest Way to Register
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    # Columns displayed on the list page
    list_display = ['name', 'slug']

# Register article model: with custom configuration
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    # Columns displayed on the list page
    list_display = ['title', 'category', 'created_at', 'updated_at']

    # Right filter bar: filter by category and time
    list_filter = ['category', 'created_at']

    # Top search box: search by title and summary
    search_fields = ['title', 'summary']

    # Hierarchical navigation of publication time (Year → Month → Day)
    date_hierarchy = 'created_at'

    # Display 20 records per page
    list_per_page = 20

Key configuration instructions for admin.py

Configuration ItemTypeFunction
list_displayListSpecify which fields to display on the list page
list_filterListAdd a filter sidebar on the right
search_fieldsListAdding a search box at the top
date_hierarchyStringNavigating by date hierarchy
list_per_pageNumbersNumber of items per page (default 100)

Refresh the admin page, click "Posts" on the left, and you can see the complete list, filtering, and search functions.


Entering test data through Admin

Click the "Add Article" button, and fill in the title, summary, and body content in the form.

The category field automatically pulls options from the existing Category (because it is set as a ForeignKey).

Enter at least 5 articles covering different categories, making it convenient to demonstrate list and filtering effects in later chapters.

The field order in the Admin form matches the order in which fields are defined in models.py. If you want to adjust the order, you can set it in the Admin class.fields = ['title', 'category', ...]Explicitly specify the ordering.


Rich text editing (optional extension)

The default body input box is a plain text field, which makes entering HTML inconvenient.

Can Installdjango-summernoteto get a WYSIWYG rich text editor:

(venv) $ pip install django-summernote

Example

# File path: blog_project/settings.py
INSTALLED_APPS = [
    # ...
    'django_summernote',   # Register App
]

# Supported File Upload Size Limit (Optional)
SUMMERNOTE_CONFIG = {
    'summernote': {
        'width': '100%',
        'height': '480',
    }
}

Then in Admin, change the content field to a Summernote editor:

Example

# File path: blog/admin.py
from django_summernote.admin import SummernoteModelAdmin

@admin.register(Post)
class PostAdmin(SummernoteModelAdmin):  # Inherit SummernoteModelAdmin
    list_display = ['title', 'category', 'created_at']
    list_filter = ['category']
    search_fields = ['title', 'summary']
    summernote_fields = ['content']     # The content field uses a rich text editor.

Now when editing articles, the body area is a full-featured rich text editor, allowing you to format, insert images, and adjust styles.

django-summernoteIt is optional. If you don't want to install a third-party library, you can completely continue writing HTML in the plain text box. The rest of this tutorial's code has nothing to do with whether Summernote is installed.


Localize the Admin interface to Chinese

If you set the following in settings.py:LANGUAGE_CODE = 'zh-hans', the Admin interface has automatically switched to Chinese.

In the model definitionverbose_nameThe parameter will be used for the field labels in the Admin form.


Chapter summary

In this chapter, you configured the Django Admin backend: created a superuser, registered models, configured list display/search/filtering, and optionally integrated a rich text editor.

Now you can visually manage article content in the browser without needing to go into the Shell to manually enter data.

other extensions