后台管理-Admin站点管理

2022-09-14 17:52:59 浏览数 (1)

admin是什么?

admin站点是可视化界面后台管理系统,方便模型数据的操作与维护。

一、配置

settings.py

在settinngs.py文件中的INSTALLED_APPS中添加’django.contrib.admin’ 默认是已经添加好的

代码语言:javascript复制
INSTALLED_APPS = [
    <span class="hljs-string">'django.contrib.admin'</span>,
]

配置中国的时区

修改settings.py文件

代码语言:javascript复制
LANGUAGE_CODE = <span class="hljs-string">'zh-Hans'</span>
TIME_ZONE = <span class="hljs-string">'Asia/Shanghai'</span>

创建管理员用户

python manage.py createsuperuser

依次输入用户名、邮箱、密码

二、所需模型类

  • 班级表
代码语言:javascript复制
<span class="hljs-comment">#  班级表</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Grades</span><span class="hljs-params">(models.Model)</span>:</span>
    gname = models.CharField(max_length=<span class="hljs-number">15</span>)
    gnum = models.IntegerField()
    ggirlnum = models.IntegerField()
    gboynum = models.IntegerField()
    isDelete = models.BooleanField(default=<span class="hljs-keyword">False</span>)
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        db_table = <span class="hljs-string">'grades'</span>

学生表

代码语言:javascript复制
<span class="hljs-comment">#学生表</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Students</span><span class="hljs-params">(models.Model)</span>:</span>
    sname = models.CharField(max_length=<span class="hljs-number">10</span>)
    ssex = models.BooleanField(default=<span class="hljs-keyword">True</span>)
    sage = models.IntegerField(default=<span class="hljs-number">20</span>)
    sgrade = models.ForeignKey(Grades, on_delete=models.CASCADE)
    isDelete = models.BooleanField(default=<span class="hljs-keyword">False</span>)
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self.sname
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        db_table = <span class="hljs-string">'students'</span>

三、admin配置属性使用

字段显示

list_display

代码语言:javascript复制
list_display = ['pk', 'gname', 'gnum', 'ggirlnum', 'gboynum', 'isDelete']

过滤字段

list_filter

代码语言:javascript复制
list_filter = ['gname']

搜索字段

search_fields

代码语言:javascript复制
search_fields = ['gname']

分页

list_per_page

代码语言:javascript复制
list_per_page = 5

更改添加、修改数据样式

fields

代码语言:javascript复制
fields = ['ggirlnum','gboynum','gname','gnum','isDelete']

添加、修改数据 属性分组

fieldsets

代码语言:javascript复制
 fieldsets = [
        ("num",{"fields":['gnum', 'ggirlnum','gboynum']}),
        ("other",{"fields":['gname','isDelete']}),
    ]

注意:fields与fieldsets不能同时使用

四、操作实例

修改admin.py文件 配置后台显示页面

代码语言:javascript复制
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Grades,Students
<span class="hljs-comment"># 注册</span>
admin.site.register(Grades)
admin.site.register(Students)

配置后台班级的页面显示

代码语言:javascript复制
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Grades,Students

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GradesAdmin</span><span class="hljs-params">(admin.ModelAdmin)</span>:</span>
    <span class="hljs-comment">#列表页属性</span>
    <span class="hljs-comment">#显示字段</span>
    list_display = [<span class="hljs-string">'pk'</span>, <span class="hljs-string">'gname'</span>, <span class="hljs-string">'gnum'</span>, <span class="hljs-string">'ggirlnum'</span>, <span class="hljs-string">'gboynum'</span>, <span class="hljs-string">'isDelete'</span>]
    <span class="hljs-comment"># 过滤字段</span>
    list_filter = [<span class="hljs-string">'gname'</span>]
    <span class="hljs-comment"># 搜索字段</span>
    search_fields = [<span class="hljs-string">'gname'</span>]
    <span class="hljs-comment">#  分页</span>
    list_per_page = <span class="hljs-number">5</span>
    <span class="hljs-comment"># 添加、修改数据的时候  属性的先后顺序</span>
    <span class="hljs-comment"># fields = ['ggirlnum','gboynum','gname','gnum','isDelete']</span>
    <span class="hljs-comment"># 添加、修改数据的时候 给属性分组</span>
    fieldsets = [
        (<span class="hljs-string">"num"</span>,{<span class="hljs-string">"fields"</span>:[<span class="hljs-string">'gnum'</span>, <span class="hljs-string">'ggirlnum'</span>,<span class="hljs-string">'gboynum'</span>]}),
        (<span class="hljs-string">"other"</span>,{<span class="hljs-string">"fields"</span>:[<span class="hljs-string">'gname'</span>,<span class="hljs-string">'isDelete'</span>]}),
    ]
admin.site.register(Grades, GradesAdmin)

关联对象

需求:在创建一个班级时可以直接添加几个学生

代码语言:javascript复制
<span class="hljs-comment">#TabularInline 横着显示添加学生布局 </span>
<span class="hljs-comment">#StackedInline  #竖着显示添加学生的布局</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StudentsInfo</span><span class="hljs-params">(admin.TabularInline)</span>:</span>    
    model = Students
    extra = <span class="hljs-number">2</span> <span class="hljs-comment">#2带表添加的学生S数</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GradesAdmin</span><span class="hljs-params">(admin.ModelAdmin)</span>:</span>
    inlines = [StudentsInfo]

字段显示中文

将模型设置属性verbose_name的值

代码语言:javascript复制
<span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> models

<span class="hljs-comment"># Create your models here.</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Grades</span><span class="hljs-params">(models.Model)</span>:</span>
    gname = models.CharField(max_length=<span class="hljs-number">15</span>, verbose_name=<span class="hljs-string">'班级名称'</span>)
    gnum = models.IntegerField(verbose_name=<span class="hljs-string">'班级总人数'</span>)
		...

说明:在将数据库里面的数据导出成csv文件,那么csv文件的表头的名字可以通过取每个字段的verbose_name来获取,数据可以通过queryset语句来获取。 这样制作出来的csv表就能像数据库一样,字段名和字段值一一对应了。 在后台字段也都会显示为中文

布尔值显示问题(显示成男女)

代码语言:javascript复制
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StudentsAdmin</span><span class="hljs-params">(admin.ModelAdmin)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">gender</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">if</span> self.ssex:
            <span class="hljs-keyword">return</span> <span class="hljs-string">"男"</span>
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> <span class="hljs-string">"女"</span>
    <span class="hljs-comment">#设置页面列的名称sa</span>
    gender.short_description = <span class="hljs-string">"性别"</span>

    list_display = [<span class="hljs-string">'pk'</span>,<span class="hljs-string">'sname'</span>,<span class="hljs-string">'sage'</span>,gender,<span class="hljs-string">'scontented'</span>,<span class="hljs-string">'sgrade'</span>,<span class="hljs-string">'isDelete'</span>]
    list_per_page = <span class="hljs-number">10</span>
    <span class="hljs-comment"># 执行动作的位置 搜索框的上下位置</span>
    actions_on_top = <span class="hljs-keyword">False</span>
    actions_on_bottom = <span class="hljs-keyword">True</span>
admin.site.register(Students,StudentsAdmin)

使用装饰器完成注册

代码语言:javascript复制
<span class="hljs-meta">@admin.register(Grades)</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StudentAdmin</span><span class="hljs-params">(admin.ModelAdmin)</span>:</span>
    <span class="hljs-keyword">pass</span>
<span class="hljs-meta">@admin.register(Students)</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StudentsAdmin</span><span class="hljs-params">(admin.ModelAdmin)</span>:</span>
	<span class="hljs-keyword">pass</span>
<span class="hljs-comment">#admin.site.register(Students,StudentsAdmin)</span>
<span class="hljs-comment"># admin.site.register(Grades, GradesAdmin)</span>

0 人点赞