Introduction
django amdin is a django admin page provided to realize the operation of data through the Web, and use django admin will need the following steps:
- Create a background administrator
- Configuring url
- Sign up and configure django admin admin page
Create a background administrator
At the command line, type:
python manage.py createsuperuser
Configuring URL
Root urls.py, usually created by default is: admin /
from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]
Sign up model
Set in the app / admin.py
from django.contrib import admin from my_app import models admin.site.register(models.Person) admin.site.register(models.Hobby)
Enter 127.0.0.1/admin, logon will be managed.
Set the table name
You can see the name on the map, it is the default you can change.
Changes in the properties of class Meta: by.
other settings
Displaying the Open Table
Persons entering
Here default returns, property name, if I want to show how age the operation.
# myapp/admin.py from django.contrib import admin from my_app import models class Myperson(admin.ModelAdmin): # 继承 list_display = ('name','age') admin.site.register(models.Person,Myperson) # 添加该类 admin.site.register(models.Hobby)
I want NAME into Chinese.
Add indexing data
from django.contrib Import ADMIN from my_app Import Models class myPerson (admin.ModelAdmin): # inherited list_display = ( ' name ' , ' Age ' ) search_fields = ( ' name ' , ' Age ' ) # can include ages admin. site.register (models.Person, myPerson) # adding the class admin.site.register (models.Hobby)
Add filter data
from django.contrib Import ADMIN from my_app Import Models class myPerson (admin.ModelAdmin): # inherited list_display = ( ' name ' , ' Age ' ) # Search search_fields = ( ' name ' , ' Age ' ) # by name, age = list_filter ( ' name ' ,) # filtered admin.site.register (models.Person, myPerson) # adding the class admin.site.register(models.Hobby)