django - (三) admin管理界面编写

django - (三) admin管理界面编写

  1. 作用

    用于管理员用户来操作的表单

  2. 编写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)
    
  3. 在setting.py的app选项中增加该app(已增加忽略)

    ‘student’,

  4. 初始化管理员用户(要求数据库已经写入到数据)

    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.
    
  5. 启动应用并登陆到admin页面

    1. python manage.py runserver 8000
    2. 登陆到admin页面 127.0.0.1:8000/admin
    3. 输入第三步中输入的用户和密码

    django - (三) admin管理界面编写_第1张图片
    django - (三) admin管理界面编写_第2张图片

  6. 配置中文界面

    LANGUAGE_CODE = 'zh-hans'
    
    TIME_ZONE = 'Asia/Shanghai'
    
    USE_I18N = True #一眼
    
    USE_L10N = True #数据和时间格式
    
    USE_TZ = True #启用时区
    

    django - (三) admin管理界面编写_第3张图片

你可能感兴趣的:(Django)