文章目录
代码目录结构
因为犯懒, 使用的本地 sqlite3 作为数据
配置文件
settings.py
# 只展示修改部分
# 静态文件的实际目录
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
逻辑代码
urls.py
from django.contrib import admin
from django.urls import path, re_path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.Login.as_view()),
# 主机管理
path('host_list/', views.host_list, name="hostList"),
path(r'host_add/', views.HostAdd.as_view()),
re_path(r'host_del/(?P<host_id>\d+)', views.host_del),
re_path(r'host_edit/(?P<host_id>\d+)', views.HostEdit.as_view()),
]
views.py(app01)
from django.shortcuts import render, redirect, HttpResponse
from django import views
from app01 import models
from django.urls import reverse
from functools import wraps # 修复装饰器
from django.utils.decorators import method_decorator # 类装饰器模块
# Create your views here.
# check login
def check_login(func):
@wraps(func)
def inner(request, *args, **kwargs):
# 判断 cookie
v = request.COOKIES.get("hao")
if v == "s21":
# 如果 cookie 存在, 执行函数
return func(request, *args, **kwargs)
else:
# 如果 cookie 不存在, 为 login 的路由拼接一个 next 参数
return redirect("/login/?next={}".format(request.path_info))
return inner
class Login(views.View):
def get(self, request):
return render(request, "login.html")
def post(self, request):
# 从 post 请求中拿到输入的用户名和密码
username = request.POST.get("username")
pwd = request.POST.get("pwd")
# 从数据库中拿到用户名密码做校验
data = models.User.objects.all().values_list("username", "password")
for i in data:
# 判断用户名密码是否匹配
if i[0] == username and i[1] == pwd:
# 判断 next 参数是否存在
if request.GET.get("next").exist():
# 若 next 参数存在, 按照 next 参数访问
rep = redirect(request.GET.get("next"))
else:
# 若不存在, 按照默认的展示
rep = redirect(reverse("hostList"))
# 将 cookie 写入
rep.set_cookie("hao", "s21", max_age=10)
return rep
else:
return render(request, "login.html")
# host list
@check_login
def host_list(request):
data = models.Host.objects.all()
return render(request, "host_list.html", {"data": data})
# host add
class HostAdd(views.View):
@method_decorator(check_login)
def get(self, request):
data = models.BusinessLine.objects.all()
return render(request, "host_add.html", {"data": data})
@method_decorator(check_login)
def post(self, request):
hostname = request.POST.get("host_name")
password = request.POST.get("host_pwd")
businessline_id = request.POST.get("host_businessline")
models.Host.objects.create(hostname=hostname, password=password, businessline_id=businessline_id)
return redirect(reverse("hostList"))
# host del
@check_login
def host_del(request, host_id):
models.Host.objects.filter(id=host_id)[0].delete()
return redirect(reverse("hostList"))
# host edit
class HostEdit(views.View):
@method_decorator(check_login)
def get(self, request, host_id):
data = models.BusinessLine.objects.all()
host = models.Host.objects.filter(id=host_id)[0]
return render(request, "host_edit.html", {"data": data, "host": host})
@method_decorator(check_login)
def post(self, request, host_id):
new_hostname = request.POST.get("host_name")
new_password = request.POST.get("host_pwd")
new_businessline = request.POST.get("host_businessline")
obj = models.Host.objects.filter(id=host_id)[0]
obj.hostname = new_hostname
obj.password = new_password
obj.businessline_id = new_businessline
obj.save()
return redirect(reverse("hostList"))
models.py
# 主机
class Host(models.Model):
hostname = models.CharField(max_length=32, unique=True)
password = models.CharField(max_length=32)
businessline = models.ForeignKey(to="BusinessLine", on_delete=models.CASCADE)
# 用户
class User(models.Model):
username = models.CharField(max_length=16, unique=True)
password = models.CharField(max_length=10)
# 业务线, 一个业务线有多台主机
class BusinessLine(models.Model):
name = models.CharField(max_length=32)
template
mom.html(母版文件)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>
{% block page_title %}
{% endblock %}
</title>
<link href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<style>
{% block page_css %}
{% endblock %}
</style>
</head>
<body>
<div class="row">
<div>
</div>
</div>
<div class="container">
<div class="row" style="margin-top: 70px">
{% block page_main %}
{% endblock %}
</div>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="/static/jQuery/jQuery3.4.1.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
{% block page_js %}
{% endblock %}
</body>
</html>
login.html
{% extends "mom.html" %}
{% block page_title %}
登录
{% endblock %}
{% block page_main %}
<form action="" method="post">
{% csrf_token %}
<div class="form-group">
<label for="username">用户名</label>
<input type="text" class="form-control" name="username" id="username" placeholder="用户名">
</div>
<div class="form-group">
<label for="pwd">密码</label>
<input type="password" class="form-control" name="pwd" id="pwd" placeholder="密码">
</div>
<button type="submit" class="btn btn-default">登录</button>
</form>
{% endblock %}
host_list.html
{% extends "mom.html" %}
{% block page_title %}
主机列表
{% endblock %}
{% block page_css %}
.table tbody tr td{
text-align: center;
vertical-align: middle;
}
.table thead tr th{
text-align: center;
vertical-align: middle;
}
{% endblock %}
{% block page_main %}
<div class="col-md-8 col-md-offset-2">
<a href="/host_add/" class="btn btn-primary" style="margin-bottom: 30px; float: right">添加主机</a>
<table class="table table-hover">
<thead>
<tr>
<th>序号</th>
<th>主机名</th>
<th>密码</th>
<th>业务线</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for foo in data %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ foo.hostname }}</td>
<td>{{ foo.password }}</td>
<td>{{ foo.businessline.name }}</td>
<td>
<a href="/host_edit/{{ foo.id }}" class="btn btn-warning">编辑</a>
<a href="/host_del/{{ foo.id }}" class="btn btn-danger">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
host_add.html
{% extends "mom.html" %}
{% block page_title %}
添加主机
{% endblock %}
{% block page_main %}
<form action="" method="post">
{% csrf_token %}
<div class="form-group">
<label for="host_name">主机名</label>
<input type="text" class="form-control" name="host_name" id="host_name" placeholder="主机名">
</div>
<div class="form-group">
<label for="host_pwd">主机密码</label>
<input type="password" class="form-control" name="host_pwd" id="host_pwd" placeholder="主机密码">
</div>
<div class="form-group">
<label for="host_businessline">所属业务线</label>
<select class="form-control" name="host_businessline" id="host_businessline">
{% for foo in data %}
<option value="{{ foo.id }}">{{ foo.name }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-default">添加</button>
</form>
{% endblock %}
host_edit.html
{% extends "mom.html" %}
{% block page_title %}
修改主机
{% endblock %}
{% block page_main %}
<form action="" method="post">
{% csrf_token %}
<div class="form-group">
<label for="host_name">主机名</label>
<input type="text" class="form-control" name="host_name" id="host_name" value="{{ host.hostname }}">
</div>
<div class="form-group">
<label for="host_pwd">主机密码</label>
<input type="password" class="form-control" name="host_pwd" id="host_pwd" value="{{ host.password }}">
</div>
<div class="form-group">
<label for="host_businessline">所属业务线</label>
<select class="form-control" name="host_businessline" id="host_businessline">
{% for foo in data %}
{% if foo == host.businessline %}
<option selected value="{{ foo.id }}">{{ foo.name }}</option>
{% else %}
<option value="{{ foo.id }}">{{ foo.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
{% endblock %}