版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gang950502/article/details/90141214
django - (三) admin管理界面编写
-
作用
用于管理员用户来操作的表单
-
编写admin模块代码(student_sys/student/admin.py)
from django.contrib import admin # Register your models here. from .models import Student class StudentAdmin(admin.ModelAdmin): #控制可以查看的字段 list_display = ('id','name','sex','profession','email', 'qq','phone','status','created_time') #控制用来过滤的字段 list_filter = ('sex','status','created_time') #控制可以用来搜索的字段 search_fields = ('name','profession') fieldsets = ( (None,{ 'fields':( 'name', ('sex','profession'), ('email','qq','phone'), 'status', ) }), ) admin.site.register(Student,StudentAdmin)
-
在setting.py的app选项中增加该app(已增加忽略)
‘student’,
-
初始化管理员用户(要求数据库已经写入到数据)
ggg@ggg-X550JX ~/myweb/student_sys $ python manage.py createsuperuser System check identified some issues: WARNINGS: student.Student.phone: (fields.W122) 'max_length' is ignored when used with IntegerField HINT: Remove 'max_length' from field Username (leave blank to use 'ggg'): ggg Email address: [email protected] Password: Password (again): This password is too short. It must contain at least 8 characters. This password is too common. This password is entirely numeric. Bypass password validation and create user anyway? [y/N]: y Superuser created successfully.
-
启动应用并登陆到admin页面
- python manage.py runserver 8000
- 登陆到admin页面 127.0.0.1:8000/admin
- 输入第三步中输入的用户和密码
-
配置中文界面
LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True #一眼 USE_L10N = True #数据和时间格式 USE_TZ = True #启用时区