1.概述:内容发布,负责添加,修改,删除内容及公告访问
2.配置admin应用
在settings文件中INSTALLED_APPS添加:'django.contrib.admin', 默认是已经添加好的
3.创建管理员用户
在项目下终端中输入:pytyhon maange.py createsuperuser
依次输入用户名,邮箱,密码
4.汉化
在settings中
LANGUAGE_CODE = 'en-us'改为:LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'UTC'改为:TIME_ZONE = 'Asia/Shanghai'
5.管理数据表
修改admin.py文件
1.先把models中的模型类注册到admin.py中
from .models import Students,Grades
#注册
admin.site.register(Grades)
admin.site.register(Students)
6.自定义管理页面
一.属性说明
#列表页面属性
1.显示字段
list_display = []
2.添加过滤器,用于过滤字段
list_filter = []
3.添加搜索字段
search_fields = []
4.添加分页,看一页显示多少
list_per_page =3
#修改,添加页面属性
注意:fields和fieldsets,只能使用一个,不能同时使用
1.规定属性的先后顺序
fields =[]
2.给属性分组
fieldsets =
举例:
from django.contrib import admin
from .models import Students,Grades
#自定义管理页面
class GradesAdmin(admin.ModelAdmin):
#列表页面属性
#1.显示字段
list_display = ['pk','gname','gdate','ggirlnum','gboynum','isdelete']
# 2.添加过滤器,用于过滤字段
list_filter = ['gname']
# 3.添加搜索字段
search_fields = ['gname']
# 4.添加分页
list_per_page =3
# #添加,修改页面属性,id即pk是没有,不可以设定顺序
# fields =['ggirlnum','gboynum','gname','gdate','isdelete']
#给属性分组,这里面的组,是一个元组
fieldsets = [
('num',{'fields':['ggirlnum','gboynum']}),
('base',{'fields':['gname','gdate','isdelete']}),
]
#注册
admin.site.register(Grades,GradesAdmin)
admin.site.register(Students)
二、关联对象
需求:在创建一个班时可以直接添加几个学生
举例:
class StudentInfo(admin.TabularInline): #StackedInline和TabularInlineg两种风格不一样
model = Students
extra = 2
#在类中直接添加
class GradesAdmin(admin.ModelAdmin):
inlines = [StudentInfo ]
#列表页面属性
#1.显示字段
list_display = ['pk','gname','gdate','ggirlnum','gboynum','isdelete']
# 2.添加过滤器,用于过滤字段
list_filter = ['gname']
# 3.添加搜索字段
search_fields = ['gname']
# 4.添加分页
list_per_page =3
# #添加,修改页面属性,id即pk是没有,不可以设定顺序
# fields =['ggirlnum','gboynum','gname','gdate','isdelete']
#给属性分组
fieldsets = [
('num',{'fields':['ggirlnum','gboynum']}),
('base',{'fields':['gname','gdate','isdelete']}),
]
class StudentsAdmin(admin.ModelAdmin):
list_display = ['pk','sname','sgender','sage','scontend','isdelete','sgrade']
list_per_page = 3
#注册
admin.site.register(Grades,GradesAdmin)
admin.site.register(Students,StudentsAdmin)
三、布尔值显示问题
例如:男女区别显示
class StudentsAdmin(admin.ModelAdmin):
#用一个方法封装
def gender(self):
if self.sgender:
return '男'
else:
return '女'
#设定页面列的名称
gender.short_description = '性别'
list_display = ['pk','sname',gender,'sage','scontend','isdelete','sgrade']
list_per_page = 3
#注册
admin.site.register(Students,StudentsAdmin)
四、执行动作位置的问题
actions_on_bottom = True
actions_on_top = False
示例:
class StudentsAdmin(admin.ModelAdmin):
def gender(self):
if self.sgender:
return '男'
else:
return '女'
#设定页面列的名称
gender.short_description = '性别'
list_display = ['pk','sname',gender,'sage','scontend','isdelete','sgrade']
list_per_page = 3
#执行动作的位置
actions_on_bottom = True
actions_on_top = False
#注册
admin.site.register(Grades,GradesAdmin)
admin.site.register(Students,StudentsAdmin)
五、不用原始方式注册,使用装饰器来进行注册
不使用:admin.site.register(Students,StudentsAdmin)注册
使用 @admin.register(Students) 注册
示例:
@admin.register(Students)
class StudentsAdmin(admin.ModelAdmin):
def gender(self):
if self.sgender:
return '男'
else:
return '女'
#设定页面列的名称
gender.short_description = '性别'
list_display = ['pk','sname',gender,'sage','scontend','isdelete','sgrade']
list_per_page = 3
#执行动作的位置
actions_on_bottom = True
actions_on_top = False