您好,登錄后才能下訂單哦!
這篇文章主要介紹基于Django的Admin后臺如何實現定制簡單監控頁,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
我們使用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 >在用設備</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 = "當前狀態" # 指定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 >{}</span>',color,ret,) elif self.flage == "False": ret = "未連接" color = "red" return format_html('<span >{}</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登錄后我們依次看一下定制效果.
主頁中的展示效果如下.
當我們需要增加自定義動作時,可以這樣寫,我們以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后臺如何實現定制簡單監控頁”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。