文章目錄
代碼目錄結構
因爲犯懶, 使用的本地 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 %}