Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement customized simple Monitoring Page in Admin background based on Django

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to customize the simple monitoring page in the Admin background based on Django. The article is very detailed and has a certain reference value. Interested friends must read it!

We use Django Admin to customize a graphical interface. First, we set the language to simplified Chinese.

Modify: settings.pyLANGUAGE_CODE = 'zh-hans'TIME_ZONE =' Asia/Shanghai' modify: apps.pyfrom django.apps import AppConfigclass MywebConfig (AppConfig): name = 'MyWeb' verbose_name = "Server parameter collection"

Then we define the models.py database model and define the basic data query.

From django.db import modelsfrom django.utils.html import format_htmlimport datetimeSTATUS_CHOICES = (('dumped,' in-use device'), ('pumped,' offline device'), ('damaged device'),) # define the basic information table of the host class HostInfoDB (models.Model): id = models.AutoField (primary_key=True) # HostAddr = models.TextField (max_length=128,verbose_name= "host address") HostAddr = models.CharField (max_length=128 Verbose_name= "host address") HostName = models.CharField (max_length=128, verbose_name= "host name") HostType = models.CharField (max_length=128, verbose_name= "system type") HostPosition = models.CharField (max_length=128, verbose_name= "computer room location") HostPlatform = models.CharField (max_length=128, verbose_name= "platform") HostGroup = models.CharField (max_length=128 Verbose_name= "grouping") HostDataTime = models.DateTimeField (verbose_name= "check date") HostUser = models.CharField (max_length=64, verbose_name= "responsible person") hostStats = models.CharField (max_length=1, choices=STATUS_CHOICES, verbose_name= "device status") def _ _ str__ (self): return self.HostName # is used to assign aliases to this table In this way, the front end is not in English class Meta (): verbose_name = "equipment list" verbose_name_plural = "equipment list" # device reuse status def Status (self): if self.hostStats = 'dumped: format_td = format_html (' in use') elif self.hostStats = = 'paired: format _ td = format_html ('offline device') elif self.hostStats = = 'damaged equipment') return format_td Status.short_description = "current status" # specify Ping test result table class HostPingInfo (models.Model): id = models.AutoField (primary_key=True) HostAddr = models.CharField (max_length=128 Verbose_name= "Host address") flage = models.CharField (max_length=64) class Meta (): verbose_name= "Survival testing" verbose_name_plural = "Survival testing" def Status (self): if self.flage = = "True": ret = "connected" color = "green" return format_html ('{}', color,ret) ) elif self.flage = "False": ret = "not connected" color = "red" return format_html ('{}', color, ret,) Status.short_description = "status" # define CPU/ memory utilization data table structure class HostCPUOrMemInfo (models.Model): id = models.AutoField (primary_key=True) HostAddr = models.CharField (max_length=128 Verbose_name= "host address") Cpu_Count = models.IntegerField () Mem_Count = models.IntegerField () class Meta (): verbose_name= "CPU memory performance" verbose_name_plural = "CPU memory performance" def Cpu_Speed (self): return format_html (', self.Cpu_Count) def Mem_Speed (self): return format_html ('' Self.Mem_Count) def Check (self): return format_html ('View', self.id) Cpu_Speed.short_description = "CPU utilization" Mem_Speed.short_description = "Mem utilization"

Continue to customize the admin.py and interact with the database view above.

From django.contrib import adminfrom MyWeb.models import * # must inherit the ModelAdmin base class before you can adjust the parameters. HostDB is the name of your table @ admin.register (HostInfoDB) class MyAdmin (admin.ModelAdmin): admin.site.site_title= "background management" admin.site.site_header = "automatic monitoring platform" # list_display = the fields you need to display should be written here, here is the field list_display = ("HostAddr") in the database "HostName", "HostType", "HostPosition", "HostDataTime", "HostPlatform", "HostGroup", "HostUser", "Status") # search_fields = to add a search box, where # search_fields = ("HostAddr", "HostType", "Status",) # list_filter = sets a filter, where the filter condition list_filter = ("HostAddr", "HostGroup",) # ordering = sets a sort condition This is sorted by id ordering = ("id",) # list_per_page = set how many records are displayed per page, default is list_per_page = 10 # list_editable = set default editable field # list_editable = ("HostName",) # date_hierarchy = Show detailed time hierarchical filtering date_hierarchy = 'HostDataTime' # readonly_fields = you can set read-only field The unmodifiable field # readonly_fields = ("hostCPU", "hostMEM",) # defines the Ping detection display field @ admin.register (HostPingInfo) class MyAdmin (admin.ModelAdmin): list_display = ("HostAddr", "flage", "Status") # defines the memory CPU utilization field @ admin.register (HostCPUOrMemInfo) class MyAdmin (admin.ModelAdmin): list_display = ("id", "HostAddr", "Cpu_Count", "Cpu_Speed", "Mem_Count") "Mem_Speed", "Check")

After saving, execute the command directly and complete the command line table operation.

Python manage.py makemigrationspython manage.py migratepython manage.py createsuperuser

After saving, start, Django and visit http://127.0.0.1:8000/admin to log in, let's take a look at the customization effect in turn.

The display effect on the home page is as follows.

When we need to add custom actions, we can write like this, let's take HostCPUOrMemInfo as an example

# define the memory CPU utilization field @ admin.register (HostCPUOrMemInfo) class MyAdmin (admin.ModelAdmin): list_display = ("id", "HostAddr", "Cpu_Count", "Cpu_Speed", "Mem_Count", "Mem_Speed") # add custom actions, where you can perform actions def func (self, request, queryset): # you can write some execution actions print (self, request) here Queryset) func.short_description = "Custom active actions" actions = [func,] # Action options are displayed at the top of the page actions_on_top = True # Action options are displayed at the bottom of the page actions_on_bottom = False # whether to display the number of selections actions_selection_counter = True

The above is all the content of the article "how to customize the simple monitoring page in the Admin background based on Django". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report