Objective
Children's shoes with the Django framework are sure to know that after the Django project is created, each app will have a urls.py file with the following lines:
From django.contrib import adminurlpatterns = [ url (R ' ^admin/', admin.site.urls),]
See the Import admin module above, that is the main content of this section
Key Features
Feature 1: Provides database management capabilities
Based on the admin module, the function of database client can be realized, and the data is added and censored.
Function 2: two times development
Based on the data management function of the module, some practical functions can be customized two times.
How to use
Step 1: In settings.py, Installed_apps, add django.contrib.admin
Installed_apps = [ ' django.contrib.admin ', ' Django.contrib.auth ', #admin依赖 ' Django.contrib.contenttypes ', #admin依赖 ' django.contrib.sessions ', #admin依赖 ... ]
Step 2:urls.py Create the admin route
From django.contrib import adminurlpatterns = [ url (R ' ^admin/', admin.site.urls), .... ]
Step 3: In the settings.py configuration file, the middleware registration
middleware = [ ' Django.contrib.sessions.middleware.SessionMiddleware ', ' Django.middleware.common.CommonMiddleware ', ' Django.contrib.auth.middleware.AuthenticationMiddleware ', ....]
Step 4: Create admin administrative user
Python manage.py Createsuperuser
Step 5: Create table information in the app's models.py file, which is the table class
From django.db import Modelsclass Gender (models. Model): name = models. Charfield (MAX_LENGTH=32) class UserInfo (models. Model): nid = Models. Autofield (primary_key=true) name = models. Charfield (max_length=30, verbose_name= ' username ', editable=false) email = models. Emailfield (db_index=true) memo = models. TextField () img = models. ImageField (upload_to= ' upload ') User_type = models. ForeignKey ("Usertype", Null=true, blank=true) gender_choices = ( (0, "male"), (1, "female"), gender = Models. Integerfield (Choices=gender_choices,default=1) class usertype (models. Model): name = models. Charfield (max_length=32) def __str__ (self): return Self.name
Step 6: Register the table in admin.py
From Django.contrib import admin# Register your models here.from app01 import modelsadmin.site.register (models.userinfo) Admin.site.register (models. usertype)
Step 7: Build the table structure
Python manage.py makemigrations && python manage.py migrate
Step 8: Run the project, login to the management interface
Python manage.py runserver Access url:http://ip:8000/admin
Final Display page
Python's rookie path: the Django admin background management feature uses