一、基础配置
配置路由
二、页面基本内容
1.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>申请管理员账号</title>
<link rel="stylesheet" href="{% static 'css/register.css' %}">
</head>
<body>
<div id="attention">
<img src="{% static 'img/网站设置.png'%}">
<b><i style="color: #1296db">欢迎来到CMDB管理中心</i></b>
<a href="/" id="login">登录</a>
</div>
<form method="post">
<img src="{% static 'img/管理员_角色管理.png' %}" alt="图片丢失">
<span id="registerTitle">申请账户</span>
<input type="text" placeholder="请输入姓名" class="inputBlock" name="name">
<input type="text" placeholder="请输入用户名" class="inputBlock" name="username">
<span class="notices">{{ user_notice }}</span>
<input type="password" placeholder="请输入密码" class="inputBlock" name="password">
<span class="notices">{{ psw_notice }}</span>
<input type="password" placeholder="再次输入密码" class="inputBlock" name="verpwd">
<span class="notices">{{ psw_notice }}</span>
<input type="text" placeholder="请输入邮箱" class="inputBlock" name="email">
<span class="notices">{{ email_notice }}</span>
<input type="text" placeholder="请输入核验码" class="inputBlock" name="vercode">
<span class="notices">{{ ver_notice }}</span>
<input type="submit" placeholder="注册" class="inputBlock" name="reg">
</form>
</body>
</html>
2.样式
*{
padding: 0;
margin: 0;
}
#attention{
height: 40px;
line-height: 40px;
font-size: 20px;
align-items: center;
padding-left: 60px;
overflow: hidden;
}
#attention>img{
position: absolute;
top: 6px;
left: 20px;
height: 30px;
width: 30px;
}
#login{
font-size: 10px;
margin-left: 80%;
}
form{
border: 1px lightgrey solid;
width: 300px;
margin: 200px auto;
padding: 5px 8px 5px 5px;
}
form>img{
display: inline-block;
height: 20px;
width: 20px;
}
#registerTitle{
display: inline-block;
color: #1296db;
position: relative;
top: -4px;
left: 5px;
}
.notices{
font-size: 10px;
color: red;
}
.inputBlock{
display: block;
height: 20px;
line-height: 20px;
width: 100%;
margin: 5px 0;
}
3.views
from django.shortcuts import render, redirect
from web.tools import searchData
import os
import django
import re
# 环境配置
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'CMDB.settings')
django.setup()
def login(request):
"""
登录页面
:param request:
:return: 账号验证无误则进入管理首页,有误则包含警告信息返回当前页
"""
# 调用数据库查询方法
rows = searchData.searchContents('users')
# 将数据库中获取的元组转为字典
users = {}
for row in rows:
users[row[0]] = row[1]
# 根据页面的访问方式进行响应
if request.method == 'GET':
return render(request, 'login.html')
else:
username = request.POST.get('username')
password = request.POST.get('password')
if username in users and password == users[username]:
return redirect('/home/')
return render(request, 'login.html', {'notice': '管理员账号或密码输入错误'})
def register(request):
"""
注册页面
:param request:
:return: 注册信息无误返回登录界面,有误则包含警告信息返回当前页
"""
if request.method == 'GET':
return render(request, 'register.html')
else:
# 获取注册信息
name = request.POST.get('name')
username = request.POST.get('username')
password = request.POST.get('password')
verpwd = request.POST.get('verpwd')
email = request.POST.get('email')
vercode = request.POST.get('vercode')
# 警告信息
user_notice = ''
psw_notice = ''
ver_notice = ''
email_notice = ''
# 正则表达式
user_pattern = r'^[a-zA-Z0-9_]+$'
psw_pattern = r'^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[@#$%^&+=])(?!.*\s).{8,}$'
email_pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
# 核验用户名
if not re.match(user_pattern, username):
user_notice = '用户名仅可由数字字母下划线组成'
# 从数据库中获取所有用户名,当前用户名存在时提示重复并重新输入
rows = searchData.searchContents('users')
if username in rows:
user_notice = '用户名不可重复'
# 核验密码,当两次密码输入相同且不为空时通过密码核验
if not (password == verpwd and password and re.match(psw_pattern, password)):
psw_notice = '请重新输入密码'
# 核验邮箱
if not re.match(email_pattern, email):
email_notice = '邮箱格式有误'
# 验证核验码,从数据库中查询核验码是否存在且未被使用
rows = searchData.searchContents('web_vercode')
# 设置验证信号,初始为False,当核验成功则置为True
flag = False
for row in rows:
if row[1] == vercode and row[2] == 'False':
# 如果核验码有效,则将该核验码失效
from web.models import VerCode
obj = VerCode.objects.get(id=row[0])
obj.status = 'True'
obj.save()
# 修改验证信号
flag = True
break
# 无效核验码时
if not flag:
ver_notice = '核验码无效'
# 当两个警告信息有一个不为空时,则说明输入有误
if psw_notice or ver_notice:
return render(request, 'register.html',
{'user_notice': user_notice, 'psw_notice': psw_notice, 'email_notice': email_notice,
'ver_notice': ver_notice, })
# 信息输入无误后,将注册信息传入申请表中,并返回登录界面
from web.models import Candidate
obj = Candidate.objects.create(
**{'name': name, 'username': username, 'password': password, 'email': email, 'verCode': vercode})
obj.save()
# 所有信息处理完毕,返回登录页
return redirect('/login')
4.效果
申请失败效果:
缓存问题解决:
有时修改css样式不发生变化,把页面的缓存关了就可以解决。