Django creates a management site for the model

Based on the model you have defined, let's create a simple management site and manage the application appropriately. Django includes a built-in management interface, which is very useful for editing content. By reading model metadata and providing a product interface for editing content, Django can automatically build a management site. The user can use it directly and configure the display mode of the model.

django.contrib.admin is already included in the INSTALLED_APPS setting, so there is no need to add it.

  1. Create super user

First, you need to create a user and manage the site. For this, you can run the following command:

python manage.py createsuperuser

The corresponding output results are shown below, where a user name, email, and password are required. (Note: The length of the password is better to be greater than 6, and it does not matter if it is less than 6, and finally y/N, only need to enter y)

Username (leave blank to use 'admin'): admin
Email address: admin@admin.com
Password: ******
Password (again): ******
Superuser created successfully.
  1. Django management site

Use the python manage.py runserver command to start the development server, and run http://127.0.0.1:8000/admin/ in the browser. The management login page is shown below.
Insert picture description here
After logging in successfully, you can see the following interface:
Insert picture description here
3. Add a model to the management site

Add your model to the management site below. For this, you can edit the admin.py file of the application as follows:

from django.contrib import admin
from .models import ##

admin.site.register(##)

Currently, reload the management site in the browser, the following figure shows the "##" model in the management site. (This is my interface)
Insert picture description here

When you register a model in the Django management site, you will get a user-friendly interface generated by the introspecting model, and then you can edit, create, and delete objects in the list in a simple way.

Finally, I hope this post can help you, I will continue to update, you can pay attention to it, thank you guys.

Guess you like

Origin blog.csdn.net/Erudite_x/article/details/112494647