欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于Django的Admin后台实现定制简单监控页

程序员文章站 2022-01-23 10:20:58
我们使用django的admin二次定制一个图形化界面,首先我们把语言设置为中文简体.修改: settings.pylanguage_code = 'zh-hans'time_zone = 'asia...

我们使用django的admin二次定制一个图形化界面,首先我们把语言设置为中文简体.

修改: settings.py
language_code = 'zh-hans'
time_zone = 'asia/shanghai'

修改: apps.py
from django.apps import appconfig
class mywebconfig(appconfig):
    name = 'myweb'
    verbose_name = "服务器参数收集"

接着我们来定义models.py数据库模型,定义基本的数据查询.

from django.db import models
from django.utils.html import format_html
import datetime

status_choices = (('d', '在用设备'), ('p', '下线设备'), ('w', '损坏设备'),)

# 定义主机基本信息表
class hostinfodb(models.model):
    id = models.autofield(primary_key=true)
    # hostaddr = models.textfield(max_length=128,verbose_name="主机地址")
    hostaddr = models.charfield(max_length=128, verbose_name="主机地址")
    hostname = models.charfield(max_length=128, verbose_name="主机名称")
    hosttype = models.charfield(max_length=128, verbose_name="系统类型")
    hostposition = models.charfield(max_length=128, verbose_name="机房位置")
    hostplatform = models.charfield(max_length=128, verbose_name="所属平台")
    hostgroup = models.charfield(max_length=128, verbose_name="分组")
    hostdatatime = models.datetimefield(verbose_name="检查日期")
    hostuser = models.charfield(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 = "设备清单"
        verbose_name_plural = "设备清单"

    # 设备再用状态
    def status(self):
        if self.hoststats == 'd':
            format_td = format_html('<span style="padding:2px;background-color:green;color:white">在用设备</span>')
        elif self.hoststats == 'p':
            format_td = format_html('<span style="padding:2px;background-color:yellow;color:black">下线设备</span>')
        elif self.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, verbose_name="主机地址")
    flage = models.charfield(max_length=64)

    class meta():
        verbose_name = "存活检测"
        verbose_name_plural = "存活检测"

    def status(self):
        if self.flage == "true":
            ret = "已连接"
            color = "green"
            return format_html('<span style="color:{};">{}</span>',color,ret,)
        elif self.flage == "false":
            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, verbose_name="主机地址")
    cpu_count = models.integerfield()
    mem_count = models.integerfield()

    class meta():
        verbose_name = "cpu内存性能"
        verbose_name_plural = "cpu内存性能"

    def cpu_speed(self):
        return format_html('<progress max="100" value="{}"></progress>',self.cpu_count)

    def mem_speed(self):
        return format_html('<progress max="100" value="{}"></progress>',self.mem_count)

    def check(self):
        return format_html('<a href="/admin/myweb/hostdb/{}/change/" rel="external nofollow" >查看</a>',self.id)

    cpu_speed.short_description = "cpu利用率"
    mem_speed.short_description = "mem利用率"

继续定制admin.py并与上方的数据库视图相交互.

from django.contrib import admin
from 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","hostuser","status")
    # search_fields = 用于添加一个搜索框,此处作为查询条件
    #search_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 makemigrations
python manage.py migrate
python manage.py createsuperuser

保存后启动,django并访问http://127.0.0.1:8000/admin登录后我们依次看一下定制效果.

基于Django的Admin后台实现定制简单监控页

主页中的展示效果如下.

基于Django的Admin后台实现定制简单监控页

当我们需要增加自定义动作时,可以这样写,我们以hostcpuormeminfo为例

# 定义内存cpu利用率字段
@admin.register(hostcpuormeminfo)
class myadmin(admin.modeladmin):
    list_display = ("id","hostaddr","cpu_count","cpu_speed","mem_count","mem_speed")

    # 添加的自定义动作,此处可执行操作
    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

基于Django的Admin后台实现定制简单监控页

以上就是基于django的admin后台实现定制简单监控页的详细内容,更多关于django admin后台定制监控页的资料请关注其它相关文章!