Django Admin initialization
When a Django project for the first time when the admin background management functions need to achieve two steps, the first step is a localized operation, the second step to create an administrator
Localization
Localized language and time zone, modify Django project settings.py file, find the following code changes:
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
The original code is commented out, 'zh-hans'
on behalf of China, Asia/Shanghai
on behalf of the Asian Shanghai, these two modified in subsequent admin interface you can achieve the Chinese
Create an administrator
The second step is to register an account administrator of the Django project in the project folder using the command line to run python manage.py createsuperuser
, in accordance with its prompt steps to complete the creation process is as follows:
E:\django\djDemo>python manage.py createsuperuser
Username (leave blank to use 'stephencurry'): lmh
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
That created as shown above, then typing python manage.py runserver
start the project, admin directory address into the project, you can enter the admin admin interface (address 127.0.0.1:8000/admin
)
After logging in with a user name and password just registered, you can enter the admin Admin Home (My new project is not so localized in English, hey hey hey)
Use admin management model
We can manage their own model by model Django's admin background management functions, but the first model will be registered in the admin class before this
Sign up model class
model model is part of the app, so we need to register the model we created in models.py in admin.py under app in here models.py has been created, the code is as follows:
from django.db import models
class BookInfo(models.Model):
btitle = models.CharField(max_length=255)
bpub_date = models.DateField()
class User(models.Model):
uname = models.CharField(max_length=30)
upassword = models.CharField(max_length=20)
ugender = models.BooleanField(default=True)
ubirth = models.DateField()
ubook = models.ForeignKey('BookInfo', on_delete=models.CASCADE)
models.py defines two model, and now we want them to be registered in the admin
Sign up model uses the admin.site.register(model)
method to register:
from django.contrib import admin
from .models import BookInfo, User
# Register your models here.
admin.site.register(BookInfo)
admin.site.register(User)
Django thermal loading capabilities, we finished modifying the code does not need to restart the project, just wait for it to finish updating refresh yourself like interface, refresh, you will see the model class in admin interface:
Then we can be manipulated to add and delete data in this interface, direct mouse clicks to complete, is not introduced!
Custom Management page (Model Management)
We can also customize the model management class to display the contents of their own definition of admin page model
It is to create a model management class in admin.py in:
class BookInfoAdmin(admin.ModelAdmin):
list_display = ['id', 'btitle', 'bpub_date']
class UserAdmin(admin.ModelAdmin):
list_display = ['uname', 'ubook']
Model Management class must extend admin.ModelAdmin
, in the model definition named management class list_display
list, and we want to admin page will be displayed in the field to write this list
After waiting for project updates refresh the page, you can see we want the admin page display fields