Background management

  • The site is divided into two parts: publishing and public access
  • It’s up to the site’s administrators to view, add, modify, and delete data in the publishing part of the content. Developing these repetitive functions can be tedious and uncreative. For this reason, Django automatically generates administrative modules based on defined model classes
  • To use Django’s admin module, follow these steps
  1. Localization of management interface
  2. Creating an Administrator
  3. Register model classes
  4. Customize the management page

1. Manage page localization

  • Localization refers to the use of local habits such as language and time display. Localization here refers to the localization of China. In mainland China, simplified Chinese is used, and the time zone is Asia/Shanghai
  • Open the test1/settings.py file, find the language code and time zone Settings, and change them to the following
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
Copy the code

2. Create an administrator

  • Create administrator code as follows, as prompted to enter the user name, email, password
python manage.py createsuperuser
Copy the code

  • Start the server
python manage.py runserver
Copy the code

  • Open the browser, enter the following address in the address box, and press Enter
http://127.0.0.1:8000/admin
Copy the code

  • Enter the username you created earlier. Password Complete Login

  • After a successful login, the interface is as follows, but there is no entry for managing books and heroes. Proceed to the third step

3. Register model classes

  • After logging in to the background management, there is no model class defined in the application created by default. You need to register the model class in the admin.py file of your application so that you can view the model class in the background management and perform CRUD operations
  • Open the booktest/admin.py file and write the following code

  • If this problem is reported incorrectly

The solution

  • Clicking on the class name “BookInfo” takes you to the list page, which by default has only one column and displays the value returned by the STR method

  • Click “Add” on the list page to enter the add page. Django generates a different form control for each model class. Fill out the form as prompted and click “Save” to complete the data creation

  • Click the first column of a row in the list page to go to the modify page

  • Modify the content as prompted, and enter the list page after the modification is successful
  • Click “Delete” on the modify page to delete an item

  • Delete: Select the check box you want to delete on the list page to delete multiple items

  • Click “Execute” to enter the confirm page, delete and return to the list page

  • All the data has been deleted, so let’s create some data manually

Customize the management page

  • Only the return value of the STR method is listed on the list page; the other properties of the object are not listed, making viewing very inconvenient
  • Django provides the ability to customize administrative pages, such as which values to display on a list page
  • Open the booktest/admin.py file and customize the class from admin.modeladmin
  • The list_display attribute indicates which attributes are to be displayed
class BookInfoAdmin(admin.ModelAdmin):
    list_display = ['pk', 'btitle', 'bpub_date']
Copy the code
  • Modify the registration code for the model class BookInfo as follows
admin.site.register(BookInfo, BookInfoAdmin)
Copy the code
  • Refresh the BookInfo list page and all properties are displayed

  • The final booktest/admin.py file code is as follows
from django.contrib import admin
from models import BookInfo,HeroInfo

class BookInfoAdmin(admin.ModelAdmin):
    list_display = ['pk', 'btitle', 'bpub_date']
class HeroInfoAdmin(admin.ModelAdmin):
    list_display = ['pk', 'hname','hgender','hcontent']

admin.site.register(BookInfo,BookInfoAdmin)
admin.site.register(HeroInfo,HeroInfoAdmin)
Copy the code