YouTip LogoYouTip

Django Blog Admin

In this chapter, you will learn how to configure Django Admin, and you can have a content management backend without writing any frontend code. * * * ## What is Admin? Django Admin is one of Django's most powerful built-in features: you only need to register models, and it automatically generates a complete backend management system. The backend provides article CRUD (Create, Read, Update, Delete), search, filtering, pagination and other features, without you having to write any CRUD code yourself. * * * ## Create Superuser First, you need to create an administrator account to log in to the backend: (venv) $ python manage.py createsuperuser Username: admin Email address: admin@example.comPassword: # Enter password (will not echo)Password (again): # Repeat passwordSuperuser 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 to Admin Open `blog/admin.py` and register the Post and Category models. ## Example # File path: blog/admin.py from django.contrib import admin from .models import Category, Post # Register Category model: simplest registration method @admin.register(Category) class CategoryAdmin(admin.ModelAdmin): # Columns to display on list page list_display =['name','slug'] # Register Post model: with custom configuration @admin.register(Post) class PostAdmin(admin.ModelAdmin): # Columns to display on 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 for publish date (year β†’ month β†’ day) date_hierarchy ='created_at' # 20 records per page list_per_page =20 ### Key admin.py Configuration Explanation | Configuration Item | Type | Function | | --- | --- | --- | | list_display | list | Specifies which fields to display on the list page | | list_filter | list | Adds filtering sidebar on the right side | | search_fields | list | Adds search box at the top | | date_hierarchy | string | Hierarchical navigation by date | | list_per_page | number | Pagination quantity per page (default 100) | Refresh the backend page, click "Articles" on the left side, and you can see the complete list, filtering, and search features. * * * ## Enter Test Data via Admin Click the "Add Article" button and fill in the title, summary, and body content in the form. The category field will automatically pull options from the already created Categories (because it's set as a ForeignKey). Enter at least 5 articles covering different categories to facilitate demonstrating list and filtering effects in subsequent chapters. > The Admin form field order is consistent with the field definition order in models.py. If you want to adjust the order, you can explicitly specify the arrangement in the Admin class by setting `fields = ['title', 'category', ...]`. * * * ## Rich Text Editor (Optional Extension) The default body input box is a plain text box, which is not convenient 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 app ] # File upload size limit (optional) SUMMERNOTE_CONFIG ={ 'summernote': { 'width': '100%', 'height': '480', } } Then change the content field to Summernote editor in Admin: ## 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']# Use rich text editor for content field Now when editing articles, the body area is a complete rich text editor where you can format text, insert images, and adjust formatting. > `django-summernote` is optional. If you don't want to install third-party libraries, you can continue writing HTML in the plain text box. The subsequent code in this tutorial is unrelated to whether Summernote is installed. * * * ## Localize Admin Interface to Chinese If `LANGUAGE_CODE = 'zh-hans'` is set in settings.py, the Admin interface will automatically switch to Chinese. The `verbose_name` parameter in model definitions will be used for field labels in Admin forms. * * * ## 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 intuitively manage article content in the browser, without needing to manually enter data in the Shell.
← Django Blog Url RoutingDjango Blog Project Init β†’