您好,登錄后才能下訂單哦!
這篇文章主要介紹基于Django的Admin后臺如何實現(xiàn)定制簡單監(jiān)控頁,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
我們使用Django的Admin二次定制一個圖形化界面,首先我們把語言設(shè)置為中文簡體.
修改: settings.py LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' 修改: apps.py from django.apps import AppConfig class MywebConfig(AppConfig): name = 'MyWeb' verbose_name = "服務(wù)器參數(shù)收集"
接著我們來定義models.py數(shù)據(jù)庫模型,定義基本的數(shù)據(jù)查詢.
from django.db import models from django.utils.html import format_html import datetime STATUS_CHOICES = (('d', '在用設(shè)備'), ('p', '下線設(shè)備'), ('w', '損壞設(shè)備'),) # 定義主機(jī)基本信息表 class HostInfoDB(models.Model): id = models.AutoField(primary_key=True) # HostAddr = models.TextField(max_length=128,verbose_name="主機(jī)地址") HostAddr = models.CharField(max_length=128, verbose_name="主機(jī)地址") HostName = models.CharField(max_length=128, verbose_name="主機(jī)名稱") HostType = models.CharField(max_length=128, verbose_name="系統(tǒng)類型") HostPosition = models.CharField(max_length=128, verbose_name="機(jī)房位置") 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="負(fù)責(zé)人") hostStats = models.CharField(max_length=1, choices=STATUS_CHOICES, verbose_name="設(shè)備狀態(tài)") def __str__(self): return self.HostName # 用于給本表指定別名,這樣前端就不是英文的了 class Meta(): verbose_name = "設(shè)備清單" verbose_name_plural = "設(shè)備清單" # 設(shè)備再用狀態(tài) def Status(self): if self.hostStats == 'd': format_td = format_html('<span >在用設(shè)備</span>') elif self.hostStats == 'p': format_td = format_html('<span >下線設(shè)備</span>') elif self.hostStats == 'w': format_td = format_html('<span >損壞設(shè)備</span>') return format_td Status.short_description = "當(dāng)前狀態(tài)" # 指定Ping檢測結(jié)果表 class HostPingInfo(models.Model): id = models.AutoField(primary_key=True) HostAddr = models.CharField(max_length=128, verbose_name="主機(jī)地址") 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 >{}</span>',color,ret,) elif self.flage == "False": ret = "未連接" color = "red" return format_html('<span >{}</span>', color, ret,) Status.short_description = "狀態(tài)" # 定義CPU/內(nèi)存利用率數(shù)據(jù)表結(jié)構(gòu) class HostCPUOrMemInfo(models.Model): id = models.AutoField(primary_key=True) HostAddr = models.CharField(max_length=128, verbose_name="主機(jī)地址") Cpu_Count = models.IntegerField() Mem_Count = models.IntegerField() class Meta(): verbose_name = "CPU內(nèi)存性能" verbose_name_plural = "CPU內(nèi)存性能" 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利用率"
繼續(xù)定制admin.py
并與上方的數(shù)據(jù)庫視圖相交互.
from django.contrib import admin from MyWeb.models import * # 必須繼承ModelAdmin基類,才可以調(diào)整參數(shù),HostDB則是你的表的名稱 @admin.register(HostInfoDB) class MyAdmin(admin.ModelAdmin): admin.site.site_title="后臺管理" admin.site.site_header = "自動化監(jiān)控平臺" # list_display = 你需要展示的字段應(yīng)該寫在這里,此處是數(shù)據(jù)庫中的字段 list_display = ("HostAddr","HostName","HostType","HostPosition","HostDataTime","HostPlatform","HostGroup","HostUser","Status") # search_fields = 用于添加一個搜索框,此處作為查詢條件 #search_fields = ("HostAddr","HostType","Status",) # list_filter = 設(shè)置一個過濾器,此處是過濾條件 list_filter = ("HostAddr","HostGroup",) # ordering = 設(shè)置一個排序條件,此處是以id作為排序依據(jù) ordering = ("id",) #list_per_page = 設(shè)置每頁顯示多少條記錄,默認(rèn)是100條 list_per_page = 10 #list_editable = 設(shè)置默認(rèn)可編輯字段 #list_editable = ("HostName",) # date_hierarchy = 顯示詳細(xì)時間分層篩選 date_hierarchy = 'HostDataTime' # readonly_fields = 可以設(shè)置只讀字段,就是無法修改的字段 #readonly_fields = ("hostCPU","hostMEM",) # 定義Ping檢測顯示字段 @admin.register(HostPingInfo) class MyAdmin(admin.ModelAdmin): list_display = ("HostAddr","flage","Status") # 定義內(nèi)存CPU利用率字段 @admin.register(HostCPUOrMemInfo) class MyAdmin(admin.ModelAdmin): list_display = ("id","HostAddr","Cpu_Count","Cpu_Speed","Mem_Count","Mem_Speed","Check")
保存后直接執(zhí)行命令,完成命令行建表操作.
python manage.py makemigrations python manage.py migrate python manage.py createsuperuser
保存后啟動,Django并訪問http://127.0.0.1:8000/admin登錄后我們依次看一下定制效果.
主頁中的展示效果如下.
當(dāng)我們需要增加自定義動作時,可以這樣寫,我們以HostCPUOrMemInfo
為例
# 定義內(nèi)存CPU利用率字段 @admin.register(HostCPUOrMemInfo) class MyAdmin(admin.ModelAdmin): list_display = ("id","HostAddr","Cpu_Count","Cpu_Speed","Mem_Count","Mem_Speed") # 添加的自定義動作,此處可執(zhí)行操作 def func(self, request, queryset): # 此處可以寫一些執(zhí)行動作 print(self, request, queryset) func.short_description = "自定義active動作" actions = [func, ] # Action選項都是在頁面上方顯示 actions_on_top = True # Action選項都是在頁面下方顯示 actions_on_bottom = False # 是否顯示選擇個數(shù) actions_selection_counter = True
以上是“基于Django的Admin后臺如何實現(xiàn)定制簡單監(jiān)控頁”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。