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('正常')
elif self.hostStats == 'p':
format_td = format_html('警告')
elif self.hostStats == 'w':
format_td = format_html('损坏')
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('正常')
elif self.HostStats == 'p':
format_td = format_html('警告')
elif self.HostStats == 'w':
format_td = format_html('损坏')
return format_td
Status.short_description = "状态"
admin.py
from django.contrib import admin
from MyWeb.models import *
# 必须继承ModelAdmin基类,才可以调整参数,HostDB则是你的表的名称
@admin.register(HostInfo)
class MyAdmin(admin.ModelAdmin):
admin.site.site_title="自动化后台管理"
admin.site.site_header = "Django 自动化运维"
# list_display = 你需要或者想要展示在页面中的字段
list_display = ("HostName","HostAddr","HostModel","HostCPU","HostMem","HostDisk","HostSys","HostZone","DataTime","HostPeple","Status")
# search_fields = 添加搜索功能,并以HostName作为搜索条件
search_fields = ("HostName",)
# ordering = 设置以HostAddr作为排序条件
ordering = ("HostAddr",)
# list_per_page = 设置每页显示的字段数
list_per_page = 10
自定义页面功能
在templates中新建一个名称为lyshark.html的文件
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% load static %}
{% block title %}{{ site_title|default:_('Django site admin') }}{% endblock %}
{% block content %}
{% endblock %}
接着在view.py中增加一条视图函数
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
@login_required(login_url="/admin/login")
def lyshark(request):
return render(request,"lyshark.html")
最后别忘了在urls.py里面添加映射记录.
from MyWeb import views
urlpatterns = [
path('admin/', admin.site.urls),
path("lyshark/",views.lyshark)
]
上方添加了装饰器,函数,只有用户登陆后才可以访问到定制页面,如果没有登陆则提示需要登陆,这样更加灵活了。
添加各种标志
添加已过期标志: 判断当前时间是否与数据库时间一致,并显示过期状态.
# name: admin.py
from django.contrib import admin
from MyWeb.models import *
@admin.register(HostDB)
class MyAdmin(admin.ModelAdmin):
list_display = ("id","date","Status")
# name: models.py
from django.db import models
from django.utils.html import format_html
import datetime
class HostDB(models.Model):
id = models.AutoField(primary_key=True)
date = models.CharField(max_length=64)
def Status(self):
NowData = datetime.date.today()
if self.date >= str(NowData):
ret = "未过期"
color = "green"
return format_html('{}',color,ret,)
else:
ret = "已过期"
color = "red"
return format_html('{}', color, ret,)
Status.short_description = "是否过期"
添加进度条展示 琢磨了一下,把进度条也堆上了。呵呵
# name: admin.py
from django.contrib import admin
from MyWeb.models import *
@admin.register(HostDB)
class MyAdmin(admin.ModelAdmin):
list_display = ("id","Count","Speed")
# name: models.py
from django.db import models
from django.utils.html import format_html
class HostDB(models.Model):
id = models.AutoField(primary_key=True)
Count = models.IntegerField()
def Speed(self):
return format_html('',self.Count)
Speed.short_description = "当前进度"
实现查看标签 添加一个查看标签
def Check(self):
return format_html('查看',self.id)