命令行输入python manage.py createsuperuser
按提示输入 用户名 邮箱地址 密码
浏览器输入http://127.0.0.1:8000/admin
进入后台管理登录界面
找到settings.py 的MIDDLEWARE (中间件)
添加'django.middleware.locale.LocaleMiddleware',
注意:不能打乱顺序
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
刷新界面
应用文件夹下admin.py
from django.contrib import admin # Register your models here. from .models import students admin.site.register(students)
增删改
修改模型
from django.db import models # Create your models here. class students(models.Model): no=models.CharField('学号',max_length=8,unique=True,primary_key=True) name=models.CharField('姓名',max_length=12) sex=models.CharField('性别',max_length=2) address=models.CharField('地址',max_length=40) # 设置后台显示的信息 def __str__(self): return str(self.name) class Meta: verbose_name='学生信息' verbose_name_plural='学生信息'
同步数据库后
开始英语的字段名变成了设置的名称
admin.py
设置list_display
from django.contrib import admin # Register your models here. from .models import students class st(admin.ModelAdmin): list_display=('name','sex','address',) admin.site.register(students,st)
admin.py中
admin.site.site_title='后台' admin.site.site_header='学生后台管理系统'
网页的title和内容中的标题变成了设置的内容