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 %} <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script> <script> $(".breadcrumbs").append(" <a href=http://www.likecs.com/\"/admin/\">仪表盘</a>"); $(".breadcrumbs").append(" <a href=http://www.likecs.com/\"/admin/\">主机监控</a>"); $(".breadcrumbs").append(" <a href=http://www.likecs.com/\"/admin/\">命令执行</a>"); $(".breadcrumbs").append(" <a href=http://www.likecs.com/\"/admin/\">图表绘制</a>"); $(".breadcrumbs").append(" <a href=http://www.likecs.com/\"/admin/\">批量CMD</a>"); </script> <form action="/lyshark/" method="post"> 用户:<input type="text" value="text"> 密码:<input type="text" value="text"> <input type="text" value="text"> <input type="text" value="text"> <input type="submit" value="提交表单"> </form> {% 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(\'<span>{}</span>\',color,ret,) else: ret = "已过期" color = "red" return format_html(\'<span>{}</span>\', 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(\'<progress max="100" value="{}"></progress>\',self.Count) Speed.short_description = "当前进度"