Django-站点管理
概述
- **内容发布:**负责添加、修改、删除内容,开发这些重复的功能是一件非常没有意义的事情,并且缺乏创造力。Django会根据定义的模型类来自动生成管理模块
- 公共访问
配置Admin应用
- 默认已经配置:
'django.contrib.admin',
创建管理员用户
1 | python manage.py createsuperuser |
按顺序输入用户名、邮箱、密码
登陆
- 启动服务
- 在浏览器地址栏输入
1
http://127.0.0.1:8000/admin
输入账号和密码
配置中文显示
1 | LANGUAGE_CODE = 'zh-Hans' |
修改settings.py文件
管理表数据
-
将要管理的表添加到应用目录下的
admin.py
文件中进行注册1
2
3
4
5
6
7
8from django.contrib import admin
# 引入要管理的模型类
from myApp.models import Student, Grade
# Register your models here.
admin.site.register(Student)
admin.site.register(Grade)
自定义管理界面
-
ModelAdmin类
-
是模型在Admin界面中的表现形式,通过定义ModelAdmin子类来定义模型在Admin界面的显示方式(
admin.py
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50from django.contrib import admin
# 引入要管理的模型类
from myApp.models import Student, Grade
# Register your models here.
class StudentAdmin(admin.ModelAdmin):
def sex(self):
if self.sex:
return "男"
else:
return "女"
sex.short_description = "性别"
# 列表页属性
list_display = ["pk", "name", "age", sex, "grade", "content", "isDelete"]
list_filter = ["grade", "age"]
search_fields = ["name"]
list_per_page = 10
# 执行动作框的位置
actions_on_top = False
actions_on_bottom = True
# 添加、修改页属性
# fields = ["name", "age", "sex", "grade", "content", "isDelete"]
fieldsets = [
("base", {"fields": ["name", "age", "sex"]}),
("more", {"fields": ["grade", "content", "isDelete"]}),
]
# admin.site.register(Student, StudentAdmin)
class StudentInLine(admin.TabularInline):
model = Student
extra = 2
class GradeAdmin(admin.ModelAdmin):
inlines = [StudentInLine]
list_display = ["pk", "name", "boyNum", "girlNum", "isDelete"]
list_filter = ["name"]
search_fields = ["name"]
list_per_page = 10
fields = ["name", "boyNum", "girlNum", "isDelete"]
# admin.site.register(Grade, GradeAdmin)
-
-
属性说明
- 列表页属性
list_display
:显示字段,可以点击进行排序list_filter
:过滤字段,过滤框出现在页面右侧search_fields
:搜索字段,搜索框会出现在列表上方list_per_page
:分页显示,确定每页有多少条数据actions_on_top
:执行动作框是否显示在上方actions_on_bottom
:执行动作框是否显示在下方
- 添加、修改页属性
fields
:属性排列顺序fieldsets
:属性分组,各组进行排序- 注意:以上两个属性不能同时使用
- 列表页属性
-
关联对象(在添加班级的时候可以直接添加几个学生)
1
2
3
4
5
6
7
8
9
10
11class StudentInLine(admin.TabularInline):
model = Student
extra = 2
class GradeAdmin(admin.ModelAdmin):
inlines = [StudentInLine]
list_display = ["pk", "name", "boyNum", "girlNum", "isDelete"]
list_filter = ["name"]
search_fields = ["name"]
list_per_page = 10
fields = ["name", "boyNum", "girlNum", "isDelete"]
admin.site.register(Grade, GradeAdmin) -
布尔值的显示问题
1
2
3
4
5
6
7
8
9
10
11
class StudentAdmin(admin.ModelAdmin):
def sex(self):
if self.sex:
return "男"
else:
return "女"
sex.short_description = "性别"
# 列表页属性
list_display = ["pk", "name", "age", sex, "grade", "content", "isDelete"] -
执行动作框的位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class StudentAdmin(admin.ModelAdmin):
def sex(self):
if self.sex:
return "男"
else:
return "女"
sex.short_description = "性别"
# 列表页属性
list_display = ["pk", "name", "age", sex, "grade", "content", "isDelete"]
list_filter = ["grade", "age"]
search_fields = ["name"]
list_per_page = 10
# 执行动作框的位置
actions_on_top = False
actions_on_bottom = True -
使用装饰器进行注册
1
2
3
class StudentAdmin(admin.ModelAdmin):
def sex(self):
重写模板
- 在
templates
目录下创建名为admin
的目录 - 找到默认模板的路径
1
C:\Users\xlg\AppData\Local\Programs\Python\Python36\Lib\site-packages\django\contrib\admin\templates\admin
- 将想要修改的模板复制到自己模板目录下的
admin
目录下就可以进行自定义修改1
2
3
4
5
6
7
8
9
10{% extends "admin/base.html" %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1>
<h1>sunck is a good man</h1>
{% endblock %}
{% block nav-global %}{% endblock %}base_site.html