14.admin后台系统

1. 配置
INSTALLED_APPS = [
    'django.contrib.admin',
]

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'
urlpatterns = [
    path('admin/', admin.site.urls),
]
2. 数据库迁移
3. 访问 127.0.0.1:8000/admin
image.png
4. 创建用户
manage.py@text > createsuperuser
5. 登陆
image.png
6. 注册

在app下的admin.py

from django.contrib import admin

from .models import Student,StudentDetail

admin.site.register(Student)
admin.site.register(StudentDetail)
image.png

image.png

image.png
7. 优化
from django.contrib import admin
from .models import Student,StudentDetail


class StudentAdmin(admin.ModelAdmin):
    #-----------------学生列表页的设置
    # 展示字段
    list_display = ['id','name','sex','age']
    # 通过某个字段跳转到单个学生界面
    list_display_links = ['name','id']
    # 自定义搜索
    search_fields = ['name','id']
    # 过滤器(以性别过滤)
    list_filter = ['sex']
    # 每页显示多少个
    list_per_page = 3


    #-----------------学生详情页的设置
    # 允许修改字段
    # fields = ['name','qq']
    # 分组(fields 与 fieldsets 只能存在一个)
    fieldsets = [
        (None,{'fields':['name']}),
        ('详细信息',{'fields':['age','sex']}),
        ('设置',{'fields':['is_delete']}),
    ]


admin.site.register(Student,StudentAdmin)

``
![image.png](https://upload-images.jianshu.io/upload_images/14920577-b8d09adc263204c1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://upload-images.jianshu.io/upload_images/14920577-61f5c66f49eec6bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(14.admin后台系统)