简单的Admin定制: 简单的过滤器使用技巧.
# name: settings.py LANGUAGE_CODE = \'zh-hans\' TIME_ZONE = \'Asia/Shanghai\' # name: apps.py from django.apps import AppConfig class MywebConfig(AppConfig): name = \'MyWeb\' verbose_name = "服务器集群" # name: 最后别忘了建库建表 python manage.py makemigrations python manage.py migrate python manage.py createsuperuser # name: models.py from django.db import models from django.utils.html import format_html STATUS_CHOICES = ((\'d\', \'正常\'),(\'p\', \'警告\'),(\'w\', \'损坏\'),) class HostInfo(models.Model): id = models.AutoField(primary_key=True) HostName = models.CharField(max_length=64,verbose_name="主机名") HostAddr = models.CharField(max_length=64,verbose_name="主机IP") DataTime = models.DateField(max_length=64,verbose_name="统计时间") HostStats = models.CharField(max_length=1,choices=STATUS_CHOICES,verbose_name="状态") def __str__(self): return self.HostName # 用于给本表指定别名,这样前端就不是英文的了 class Meta(): verbose_name = "Web前台集群" verbose_name_plural= "Web前台集群" # 自定义方法,主要负责给主机标注颜色 def Status(self): if self.HostStats == \'d\': format_td = format_html(\'<span>正常</span>\') elif self.HostStats == \'p\': format_td = format_html(\'<span>警告</span>\') elif self.HostStats == \'w\': format_td = format_html(\'<span>损坏</span>\') return format_td Status.short_description = "状态" # name: admin.py from django.contrib import admin from MyWeb.models import * # 必须继承ModelAdmin基类,才可以调整参数,HostDB则是你的表的名称 @admin.register(HostInfo) class MyAdmin(admin.ModelAdmin): admin.site.site_title="自动化后台管理" admin.site.site_header = "Django 管理平台" # list_display = 你需要或者想要展示在页面中的字段 list_display = ("HostName","HostAddr","DataTime","Status") # search_fields = 添加搜索功能,并以HostName,HostAddr作为搜索条件 search_fields = ("HostName","HostAddr",) # ordering = 设置以id号作为排序条件 ordering = ("id",) # list_per_page = 设置每页显示数据条数 list_per_page = 10 # list_filter = 设置一个过滤器,此处是以hostname作为过滤条件 list_filter = ("HostName",) #list_editable = 设置默认可编辑字段 list_editable = ("DataTime",) # date_hierarchy = 显示详细时间分层筛选 date_hierarchy = \'DataTime\' # readonly_fields = 可以设置只读字段,就是无法修改的字段 readonly_fields = ("HostAddr",) # list_display_links 指定点击HostAddr进入编辑状态 list_display_links = ("HostAddr",)添加已过期标志: 通过取出当前时间与数据库中现有时间对比,来实现是否过期.
# name: admin.py from django.contrib import admin from MyWeb.models import * @admin.register(HostDB) class MyAdmin(admin.ModelAdmin): list_display = ("id","date","Status") # name: models.py from django.db import models from django.utils.html import format_html import datetime class HostDB(models.Model): id = models.AutoField(primary_key=True) date = models.CharField(max_length=64) def Status(self): NowData = datetime.date.today() if self.date >= str(NowData): ret = "未过期" color = "green" return format_html(\'<span>{}</span>\',color,ret,) else: ret = "已过期" color = "red" return format_html(\'<span>{}</span>\', color, ret,) Status.short_description = "是否过期"添加进度条展示: HTML5默认支持进度条标签,使用progress我们直接使用这个标签就好了.
# name: admin.py from django.contrib import admin from MyWeb.models import * @admin.register(HostDB) class MyAdmin(admin.ModelAdmin): list_display = ("id","Count","Speed") # name: models.py from django.db import models from django.utils.html import format_html class HostDB(models.Model): id = models.AutoField(primary_key=True) Count = models.IntegerField() def Speed(self): return format_html(\'<progress max="100" value="{}"></progress>\',self.Count) Speed.short_description = "当前进度"增加自定义actions: 自定义Action标签,Action标签就是Admin页面中左上角的横线部分,我们自己增加新的.
# name: admin.py from django.contrib import admin from MyWeb.models import * # 必须继承ModelAdmin基类,才可以调整参数,HostDB则是你的表的名称 @admin.register(HostInfo) class MyAdmin(admin.ModelAdmin): admin.site.site_title="自动化后台管理" admin.site.site_header = "Django 管理平台" <省略部分...> def func(self,request,queryset): # 此处可以写一些执行动作 print(self,request,queryset) func.short_description = "自定义active动作" actions = [func,] # Action选项都是在页面上方显示 actions_on_top = True # Action选项都是在页面下方显示 actions_on_bottom = False # 是否显示选择个数 actions_selection_counter = True