我们使用django的admin二次定制一个图形化界面,首先我们把语言设置为中文简体.
修改: ttings.pylanguage_code = 'zh-hans'time_zone = 'asia/shanghai'修改: apps.pyfrom django.apps import appconfigclass mywebconfig(appconfig): name = 'myweb' verbo_name = "服务器参数收集"
接着我们来定义models.py数据库模型,定义基本的数据查询.
from django.db import modelsfrom django.utils.html import format_htmlimport datetimestatus_choices = (('d', '在用设备'), ('p', '下线设备'), ('w', '损坏设备'),)# 定义主机基本信息表class hostinfodb(models.model): id = models.autofield(primary_key=true) # hostaddr = models.textfield(max_length=128,verbo_name="主机地址") hostaddr = models.charfield(max_length=128, verbo_name="主机地址") hostname = models.charfield(max_length=128, verbo_name="主机名称") hosttype = models.charfield(max_length=128, verbo_name="系统类型") hostposition = models.charfield(max_length=128, verbo_name="机房位置") hostplatform = models.charfield(max_length=128, verbo_name="所属平台") hostgroup = models.charfield(max_length=128, verbo_name="分组") hostdatatime = models.datetimefield(verbo_name="检查日期") hostur = models.charfield(max_length=64, verbo_name="负责人") host1935年12月召开的什么会议stats = models.charfield(max_length=1, choices=status_choices, verbo_name="设备状态") def __str__(lf): return lf.hostname # 用于给本表指定别名,这样前端就不是英文的了 class meta(): verbo_name = "设备清单" verbo_name_plural = "设备清单" # 设备再用状态 def status(lf): if lf.hoststats == 'd': format_td = format_html('<span style="padding:2px;background-color:green;color:white">在用设备</span>') elif lf.hoststats == 'p': format_td = format_html('<span style="padding:2px;background-color:yellow;color:black">下线设备</span>') elif lf.hoststats == 'w'家长教育理念: format_td = format_html('<span style="padding:2px;background-color:red;color:white">损坏设备</span>') return format_td status.short_description = "当前状态"# 指定ping检测结果表class hostpinginfo(models.model): id = models.autofield(primary_key=true) hostaddr = models.charfield(max_length=128, verbo_name="主机地址") flage = models.charfield(max_length=64) class meta(): verbo_name = "存活检测" verbo_name_plural = "存活检测" def status(lf): if lf.flage == "true": ret = "已连接" color = "green" return format_html('<span style="color:{};">{}</span>',color,ret,) elif lf.flage == "fal": ret = "未连接" color = "red" return format_html('<span style="color:{};">{}</span>', color, ret,) status.short_description = "状态"# 定义cpu/内存利用率数据表结构class hostcpuormeminfo(models.model): id = models.autofield(primary_key=true) hostaddr = models.charfield(max_length=128, verbo_name="主机地址") cpu_count = models.integerfield() mem_count = models.integerfield() class meta(): verbo_name = "cpu内存性能" verbo_name_plural = "cpu内存性能" def cpu_speed(lf): return format_html('<progress max="100" value="{}"></progress>',lf.cpu_count) def mem_speed(lf): 东坡诗词 return format_html('<progress max="100" value="{}"></progress>',lf.mem_count) def check(lf): return format_html('<a href="/admin/myweb/hostdb/{}/change/" rel="external nofollow" >查看</a>',lf.id) cpu_speed.short_description = "cpu利用率" mem_speed.short_description = "mem利用率"
继续定制admin.py
并与上方的数据库视图相交互.
from django.contrib import adminfrom myweb.models import *# 必须继承modeladmin基类,才可以调整参数,hostdb则是你的表的名称@admin.register(hostinfodb)class myadmin(admin.modeladmin): admin.site.site_title="后台管理" admin.site.site_header = "自动化监控平台" # list_display = 你需要展示的字段应该写在这里,此处是数据库中的字段 list_display = ("hostaddr","hostname","hosttype","hostposition","hostdatatime","hostplatform","hostgroup","hostu朱自清散文代表作r","status") # arch_fields = 用于添加一个搜索框,此处作为查询条件 #arch_fields = ("hostaddr","hosttype","status",) # list_filter = 设置一个过滤器,此处是过滤条件 list_filter = ("hostaddr","hostgroup",) # ordering = 设置一个排序条件,此处是以id作为排序依据 ordering = ("id",) #list_per_page = 设置每页显示多少条记录,默认是100条 list_per_page = 10 #list_editable = 设置默认可编辑字段 #list_editable = ("hostname",) # date_hierarchy = 显示详细时间分层筛选 date_hierarchy = 'hostdatatime' # readonly_fields = 可以设置只读字段,就是无法修改的字段 #readonly_fields = ("hostcpu","hostmem",)# 定义ping检测显示字段@admin.register(hostpinginfo)class myadmin(admin.modeladmin): list_display = ("hostaddr","flage","status")# 定义内存cpu利用率字段@admin.register(hostcpuormeminfo)class myadmin(admin.modeladmin): list_display = ("id","hostaddr","cpu_count","cpu_speed","mem_count","mem_speed","check")
保存后直接执行命令,完成命令行建表操作.
python manage.py makemigrationspython manage.py migratepython manage.py createsuperur
保存后启动,django并访问http://127.0.0.1:8000/admin登录后我们依次看一下定制效果.
主页中的展示效果如下.
当我们需要增加自定义动作时,可以这样写,我们以hostcpuormeminfo
为例
# 定义内炒冬瓜的做法存cpu利用率字段@admin.register(hostcpuormeminfo)class myadmin(admin.modeladmin): list_display = ("id","hostaddr","cpu_count","cpu_speed","mem_count","mem_speed") # 添加的自定义动作,此处可执行操作 def func(lf, request, queryt): # 此处可以写一些执行动作 print(lf, request, queryt) func.short_description = "自定义active动作" actions = [func, ] # action选项都是在页面上方显示 actions_on_top = true # action选项都是在页面下方显示 actions_on_bottom = fal # 是否显示选择个数 actions_lection_counter = true
以上就是基于django的admin后台实现定制简单监控页的详细内容,更多关于django admin后台定制监控页的资料请关注www.887551.com其它相关文章!
本文发布于:2023-04-04 00:15:14,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/812606529334fac16222797db750ded4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:基于Django的Admin后台实现定制简单监控页.doc
本文 PDF 下载地址:基于Django的Admin后台实现定制简单监控页.pdf
留言与评论(共有 0 条评论) |