Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Django Admin — Managing Blog Posts

Django Admin — Managing Blog Posts

In this chapter you will learn to configure Django Admin and have a content management backend without writing front-end code.


What is Admin?

Django Admin is one of Django's most powerful built-in features: you only need to register a model, and it automatically generates a complete backend management system.

The backend provides functions such as creating, reading, updating, deleting, searching, filtering, and paginating posts, without you writing any CRUD code.


Create a superuser

First you need to create an administrator account to log in to the backend:



(venv) $ python manage.py createsuperuser

用户名:admin

电子邮件地址:admin@runoob.com

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


Register models with Admin

Open blog/admin.py, register the Post and Category models.

Example

File path: blog/admin.py

from

django.

contrib

import

admin

from

.

models

import

Category

,

Post

Register the category model: the simplest registration method

@

admin.

register

(

Category

)

class

CategoryAdmin

(

admin.

ModelAdmin

)

:

   

Columns displayed on the list page

    list_display

=

[

'name'

,

'slug'

]

Register the post 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-side 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 notes for admin.py

Configuration Item Type Purpose
list_display List Specify which fields are displayed on the list page
list_filter List Add a filtering sidebar on the right
search_fields List Add a search box at the top
date_hierarchy String Navigate by date hierarchy
list_per_page Digit Number of items per page (default 100)

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


Enter test data through Admin

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

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

Enter at least 5 posts covering different categories, to facilitate demonstrating list and filter effects in later chapters.

The order of Admin form fields is consistent with the order of field definitions in models.py. If you want to adjust the order, you can set it in the Admin class with fields = ['title', 'category', ...] to specify the order explicitly.


Rich text editing (optional extension)

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

You can install django-summernote to get a WYSIWYG rich text editor:



(venv) $ pip install django-summernote

Example

File path: blog_project/settings.py

INSTALLED_APPS

=

[

   

...

   

'django_summernote'

,

 

Register the 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 from 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 a post, the body area is a full rich text editor, allowing you to format text, insert images, and adjust the layout.

django-summernote is optional. If you don't want to install a third-party library, it is perfectly fine to continue writing HTML in a plain text box. The code in later tutorials is independent of whether Summernote is installed.


Localizing the Admin interface

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

The verbose_name parameter in the model definition is used for the field labels in the Admin form.


Chapter Summary

In this chapter you configured the Django Admin backend: creating a superuser, registering models, configuring list display/search/filter, and optionally integrating a rich text editor.

Now you can intuitively manage post content in the browser, without entering data manually in the Shell.