Three management background management data model of learning Django

Django built a management background, this dynamic management background read into the data model, and then create a comprehensive management interface, so you can easily manage data. This is an "brought on by" a convenient tool.
Management background is actually a function of the application is called django.contrib.admin, it is included in INSTALLED_APPS default settings.

First, create a superuser

To use the management background, you need to register a super user manage.py createsuperuser Python
D: \ Work \ code \ Python \ django_01> Python manage.py createsuperuser
the Username (the Leave blank to use 'yinww'): ADMIN enter your administrator name
Email address: [email protected] enter the mailbox
password: enter the password
password (again): enter the password again
Superuser created successfully.
If the password is too simple and too weak to be prompted to enter the password, confirm whether to continue to create user

Two, Django management background

启动服务 python manage.py runserver
D:\work\code\python\django_01>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
June 16, 2019 - 10:13:16
Django version 2.2.2, using settings 'django_01.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Access http://127.0.0.1:8000/admin/ go to the admin login page, then enter the super user name and password to log created above management background

Sign in to see the User Group and management background, can add, delete, change search Group object. This is because INSTALLED_APPS settings.py file configured django.contrib.auth

Third, add background model management

Let management background to manage the Post model, we need to add the following in admin.py blog application:

from django.contrib import admin
from .models import Post

admin.site.register(Post)

Refresh the page management background, you can see also manage the Post model.

Fourth, management background in custom model display

from django.contrib import admin
from .models import Post
@admin.register(Post)

class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug', 'author', 'publish', 'status')
    list_filter = ('status', 'created', 'publish', 'author')
    search_fileds = ('title', 'body')
    prepopulated_fields = {'slug':('title',)}
    raw_id_fields = ('author',)
    date_hierarchy = 'publish'
    ordering = ('status', 'publish')

Then refresh the page, enter the page Posts Post can be found in the display shows the model has been defined according to the content above.

This concludes this article, it may be more concerned about the number of public and personal micro signal:

Guess you like

Origin blog.csdn.net/bowei026/article/details/92396134