Django最强大的部分之一是自动管理界面。它从模型中读取元数据,以提供一个快速的,以模型为中心的界面,受信任的用户可以在其中管理您网站上的内容。管理员的建议用法仅限于组织的内部管理工具。它并非旨在构建您的整个前端。
Django Admin 简单配置settings.py
LANGUAGE_CODE = \'zh-hans\' TIME_ZONE = \'Asia/Shanghai\'models.py
from django.db import models import sqlite3 from django.utils.html import format_html STATUS_CHOICES = ((\'d\', \'正常\'),(\'p\', \'警告\'),(\'w\', \'损坏\'),) class HostDB(models.Model): id = models.AutoField(primary_key=True) hostname = models.CharField(max_length=64,verbose_name="主机名称") hostaddr = models.TextField(max_length=64,verbose_name="主机地址") hostCPU = models.CharField(max_length=64,verbose_name="主机CPU") hostMEM = models.CharField(max_length=64,verbose_name="主机内存") DataTime = 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 # 自定义方法,主要负责给主机标注颜色 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 = "当前状态"admin.py
from django.contrib import admin from MyWeb.models import * # 必须继承ModelAdmin基类,才可以调整参数,HostDB则是你的表的名称 @admin.register(HostDB) class MyAdmin(admin.ModelAdmin): admin.site.site_title="自动化后台管理" admin.site.site_header = "Django 自动化运维" # list_display = 你需要展示的字段应该写在这里,此处是数据库中的字段 list_display = ("hostname","hostaddr","hostCPU","hostMEM","DataTime","hostUser","Status") # search_fields = 用于添加一个搜索框,此处以hostaddr作为查询条件 search_fields = ("hostaddr",) # list_filter = 设置一个过滤器,此处是以hostname作为过滤条件 list_filter = ("hostname",) # ordering = 设置一个排序条件,此处是以id作为排序依据 ordering = ("id",) #list_per_page = 设置每页显示多少条记录,默认是100条 list_per_page = 10 #list_editable = 设置默认可编辑字段 #list_editable = ("DataTime",) # date_hierarchy = 显示详细时间分层筛选 date_hierarchy = \'DataTime\' # readonly_fields = 可以设置只读字段,就是无法修改的字段 readonly_fields = ("hostCPU","hostMEM",) python manage.py makemigrations python manage.py migrate python manage.py createsuperuser效果如下。
另外找到一个不错的主题
pip install simpleui vim settings.py INSTALLED_APPS = [ \'simpleui\', \'django.contrib.admin\', \'myweb.apps.MywebConfig\', ] STATIC_URL = \'/static/\' STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), ] 定制主机管理菜单 vim 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 STATUS_CHOICES = ((\'d\', \'正常\'),(\'p\', \'警告\'),(\'w\', \'损坏\'),) class HostInfo(models.Model): id = models.AutoField(primary_key=True) HostName = models.CharField(max_length=64,verbose_name="主机名") HostAddr = models.CharField(max_length=64,verbose_name="主机IP") HostModel = models.CharField(max_length=64,verbose_name="设备型号") HostCPU = models.CharField(max_length=64,verbose_name="主机CPU") HostMem = models.CharField(max_length=64,verbose_name="主机内存") HostDisk = models.CharField(max_length=64,verbose_name="主机磁盘") HostSys = models.CharField(max_length=64,verbose_name="操作系统") HostZone = models.CharField(max_length=64,verbose_name="区域") DataTime = models.DateField(max_length=64,verbose_name="统计时间") HostPeple = 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 = "Web前台集群" verbose_name_plural= "Web前台集群" # 自定义方法,主要负责给主机标注颜色 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 = "状态"