Admin站点是django提供的一个后台管理页面,可以用来对用户与数据库表数据进行管理。
Admin站点配置流程
- 1.在settings.py文件中INSTALL_APPS列表中添加
django.contrib.admin
,django默认添加。 - 2 . 创建管理员用户,在
terminal
窗口创建管理员用户,python manage.py createsuperuser
,创建成功后可以用账户访问127.0.0.1:8000/admin
管理页。
(3). 定义模型在页面显示的列表页属性和修改页属性。
- **列表页属性**
(1). list_display 用于显示字段
list_display = ['pk','s_name','s_age','s_gender,'contend','s_grade','isDelete]
**注意**: 如果直接写数据模型种的字段名,那么在显示字段的标题也为字段名,如果要将标题改为中文,则需要如下定义
def name(self):
return self.s_name
name.short_description = '姓名' # 设置name函数的描述为姓名
list_display = [name] # 元素直接写函数的名字
<div align="center"><img src="https://img2018.cnblogs.com/blog/1514724/201810/1514724-20181028120445616-1859053195.png" title="效果"></div>
(2). list_filter 过滤器 :用于设置页面显示的数据以什么分组
list_filter = ['s_grade"] # 设置以班级作为显示的分组
<div align="center"><img src="https://img2018.cnblogs.com/blog/1514724/201810/1514724-20181028120829154-415189991.png" title="效果"></div>
(3) . search_fields 用于页面关键字搜索
search_fields = ['s_name'] # 以名字搜索
<div align="center"><img src="https://img2018.cnblogs.com/blog/1514724/201810/1514724-20181028121013076-492905196.png" title="效果"></div>
(4). list_per_page 用于分页
list_per_page = 3 # 设置一页显示3条数据
<div align="center"><img src="https://img2018.cnblogs.com/blog/1514724/201810/1514724-20181028121133446-1844621034.png" title="效果"></div>
- **修改页属性**
(1).fields 用于决定页面显示字段的先后顺序
fields = ['g_name', 'boy_num', 'girl_num', 'create_date', 'isDelete'] # 更新和添加修改页的字段显示会以列表中元素的顺序
(2). fieldsets 用于给页面显示的字段进行分组
fieldsets = [('基本信息',{'fields':['s_name','s_age','s_gender','s_grade']}), # 将这些字段分组显示,组名为基本信息
('拓展信息',{'fields':['contend','isDelete']})]
<div align="center"><img src="https://img2018.cnblogs.com/blog/1514724/201810/1514724-20181028122035579-784161683.png" title="效果"></div>
> 注意:fields与fieldsets不能同时使用
- **执行动作的位置**
actions_on_bottom = True
actions_on_top = False
<div align="center"><img src="https://img2018.cnblogs.com/blog/1514724/201810/1514724-20181028122226434-1346420037.png" title="效果"></div>
---
- **自定义管理界面**
实现关联,在创建班级的时候添加两个学生
定义关联,在创建班级的时候添加2个学生
class StudentsAdd(admin.TabularInline):
model = Students
extra = 2
在班级模型添加关联
inlines = [StudentsAdd]
<div align="center"><img src="https://img2018.cnblogs.com/blog/1514724/201810/1514724-20181028122417499-1714150045.png" title="效果"></div>
---
#### 全部实现代码
from django.contrib import admin
from user.models import Grades,Students # 从模块引入模型
定义关联,在创建班级的时候添加2个学生
class StudentsAdd(admin.TabularInline):
model = Students
extra = 2 # 设置为2
class GradesAdmin(admin.ModelAdmin):
# 列表页属性
def name(self):
return self.g_name
def girl_num(self):
return self.girl_num
def boy_num(self):
return self.boy_num
def time(self):
return self.create_date
def delete(self):
return self.isDelete
name.short_description = '班级名'
girl_num.short_description = '女生人数'
boy_num.short_description = '男生人数'
time.short_description = '创建时间'
delete.short_description = '是否删除'
inlines = [StudentsAdd]
list_display = ['pk',name,girl_num,boy_num,time,delete] # 用于显示标题为中文
list_filter = ['g_name']
search_fields = ['g_name']
list_per_page = 2
# # 添加修改页属性
# fields = ['g_name', 'boy_num', 'girl_num', 'create_date', 'isDelete']
fieldsets = [('num', {'fields': ['boy_num', 'girl_num']}),
('base', {'fields': ['g_name', 'create_date', 'isDelete']})]
admin.site.register(Grades,GradesAdmin)
装饰器实现注册
@admin.register(Students)
class StudentsAdmin(admin.ModelAdmin):
def gender(self):
if self.s_gender:
return '男'
else:
return '女'
def name(self):
return self.s_name
def age(self):
return self.s_age
def content(self):
return self.contend
def grade(self):
return self.s_grade
def delete(self):
return self.isDelete
gender.short_description = '性别'
name.short_description = '姓名'
age.short_description = '年龄'
content.short_description = '描述'
grade.short_description = '班级'
delete.short_description = '是否删除'
list_display = ['pk',name,age,gender,content,grade,delete]
list_filter = ['s_grade']
search_fields = ['s_name']
list_per_page = 3
fieldsets = [('基本信息',{'fields':['s_name','s_age','s_gender','s_grade']}),
('拓展信息',{'fields':['contend','isDelete']})]
# 执行动作的位置
actions_on_bottom = True
actions_on_top = False
admin.site.register(Students,StudentsAdmin)