目录
templates/admin_role/admin_list.html
编辑
templates/admin_role/add_modify.html
views/admin_role.py
账号管理---->
账号添加
views.py
身份修改
views.py
重置密码
views.py
templates/admin_role/admin_list.html
{% extends "index/index.html" %}
{% block content %}
<div class="container">
<div style="margin-bottom: 10px">
<a class="btn btn-success" href="/add/admin/"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>添加账号</a>
</div>
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title">账号列表</h3>
</div>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
<th>重置密码</th>
<th>身份</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for data in data_list %}
<tr>
<th>{{ data.id }}</th>
<td>{{ data.username }}</td>
<td>******</td>
<td><a href="/reset/{{ data.id }}/admin/">重置密码</a></td>
<td>{{data.get_role_display}}</td>
<td>
<a href="/modify/{{ data.id}}/admin/"><span style="color: green" class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
<a href="/del/{{ data.id}}/admin/"><span style="color: red" class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock %}
templates/admin_role/add_modify.html
{% extends "index/index.html" %}
{% load static %}
{% block css %}
<link rel="stylesheet" href="{% static 'css/layui.css' %}">
{% endblock %}
{% block content %}
<div class="container">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">{{ title }}</h3>
</div>
<div class="panel-body">
<div class="form-group">
<form method="post" novalidate>
{% csrf_token %}
{% for item in form %}
<label for="exampleInputEmail1" class="col-sm-2" >{{ item.label }}</label>
{{ item }}
<span style="color: red">{{ item.errors.0 }}</span>
{% endfor %}
<button type="submit" class="btn btn-success">提交</button>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
views/admin_role.py
# -*- coding:utf-8 -*-
from django.shortcuts import render, redirect
from django.core.validators import ValidationError
from django import forms
from demo_one import models
from demo_one.utils import pwd_data
def admin_list(request):
data_list = models.Adminrole.objects.all()
content = {
"data_list": data_list
}
return render(request, "admin_role/admin_list.html",content)
class AdminModelForm(forms.ModelForm):
# 创建一个确认密码的输入框,然后将input类型修改为password
confirm_password = forms.CharField(label="确认密码", widget=forms.PasswordInput)
class Meta:
model = models.Adminrole
fields = ["username", "password", "confirm_password", "role"]
widgets = {
# render_value=True 保留这个字段的值
"password": forms.PasswordInput(render_value=True)
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
field.widget.attrs = {"class": "form-control", "autocomplete": "off"}
# 钩子方法,处理改字段
# 将第一次输入的密码进行加密处理
def clean_password(self):
# 获取用户框输入的该字段
pwd = self.cleaned_data.get("password")
# print(f"第一次获取的password:{pwd}")
# print("*-*"*30)
return pwd_data.md5(pwd)
# 将确认密码也进行同样的加密方式
def clean_confirm_password(self):
confirm = pwd_data.md5(self.cleaned_data.get("confirm_password"))
# 获取到第一次数据的密码(加密后的)
pwd = self.cleaned_data.get("password")
# print(f"加密之后的确认密码:{confirm}")
# print(f"第二次获取输入的密码:{pwd}")
if confirm != pwd:
raise ValidationError("两次密码不一致")
# 将需要存储的字段返回
return confirm
def add_admin(request):
title = "账号添加"
if request.method == "GET":
form = AdminModelForm()
content = {
"title": title,
"form": form
}
return render(request, "admin_role/add_modify.html", content)
form = AdminModelForm(data=request.POST)
if form.is_valid():
form.save()
return redirect("/admin/list/")
return render(request, "admin_role/add_modify.html", {"title": title, "form": form})
class AdminModifyModelForm(forms.ModelForm):
class Meta:
model = models.Adminrole
fields = ["username", "role"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
field.widget.attrs = {"class": "form-control", "autocomplete": "off"}
# 修改账户和权限
def modify_admin(request, nid):
title = "编辑页面"
title_obj = models.Adminrole.objects.filter(id=nid).first()
if request.method == "GET":
form = AdminModifyModelForm(instance=title_obj)
return render(request, "admin_role/add_modify.html",{"form":form, "title":title})
form = AdminModifyModelForm(instance=title_obj,data=request.POST)
if form.is_valid():
form.save()
return redirect("/admin/list/")
return render(request, "admin_role/add_modify.html", {"form": form, "title": title})
def del_admin(request, nid):
models.Adminrole.objects.filter(id=nid).delete()
return redirect("/admin/list/")
class AdminResetModelForm(forms.ModelForm):
# 创建一个确认密码的输入框,然后将input类型修改为password
confirm_password = forms.CharField(label="确认密码", widget=forms.PasswordInput)
class Meta:
model = models.Adminrole
fields = ["password"]
widgets = {
# render_value=True 保留这个字段的值
"password": forms.PasswordInput(render_value=True)
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
field.widget.attrs = {"class": "form-control", "autocomplete": "off"}
# 钩子方法,处理改字段
# 将第一次输入的密码进行加密处理
def clean_password(self):
# 获取用户框输入的该字段
pwd = self.cleaned_data.get("password")
# print(f"第一次获取的password:{pwd}")
# print("*-*"*30)
return pwd_data.md5(pwd)
# 将确认密码也进行同样的加密方式
def clean_confirm_password(self):
confirm = pwd_data.md5(self.cleaned_data.get("confirm_password"))
# 获取到第一次数据的密码(加密后的)
pwd = self.cleaned_data.get("password")
# print(f"加密之后的确认密码:{confirm}")
# print(f"第二次获取输入的密码:{pwd}")
if confirm != pwd:
raise ValidationError("两次密码不一致")
# 将需要存储的字段返回
return confirm
# 重置密码
def reset_admin(request, nid):
title_obj = models.Adminrole.objects.filter(id=nid).first()
title = f"重置《{title_obj.username}》密码"
if request.method == "GET":
form = AdminResetModelForm()
return render(request, "admin_role/add_modify.html",{"form":form, "title":title})
form = AdminResetModelForm(data=request.POST,instance=title_obj)
if form.is_valid():
form.save()
return redirect("/admin/list/")
return render(request, "admin_role/add_modify.html", {"form": form, "title": title})
账号管理---->
账号添加
-
views.py
class AdminModelForm(forms.ModelForm): # 创建一个确认密码的输入框,然后将input类型修改为password confirm_password = forms.CharField(label="确认密码", widget=forms.PasswordInput) class Meta: model = models.Adminrole fields = ["username", "password", "confirm_password", "role"] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for name, field in self.fields.items(): field.widget.attrs = {"class": "form-control", "autocomplete": "off"} # 钩子方法,处理改字段 # 将第一次输入的密码进行加密处理 def clean_password(self): # 获取用户框输入的该字段 pwd = self.cleaned_data.get("password") # print(f"第一次获取的password:{pwd}") # print("*-*"*30) return pwd_data.md5(pwd) # 将确认密码也进行同样的加密方式 def clean_confirm_password(self): confirm = pwd_data.md5(self.cleaned_data.get("confirm_password")) # 获取到第一次数据的密码(加密后的) pwd = self.cleaned_data.get("password") # print(f"加密之后的确认密码:{confirm}") # print(f"第二次获取输入的密码:{pwd}") if confirm != pwd: raise ValidationError("两次密码不一致") # 将需要存储的字段返回 return confirm def add_admin(request): title = "账号添加" if request.method == "GET": form = AdminModelForm() content = { "title": title, "form": form } return render(request, "admin_role/add_admin.html", content) form = AdminModelForm(data=request.POST) if form.is_valid(): form.save() return redirect("/admin/list/") return render(request, "admin_role/add_admin.html", {"title": title, "form": form})
身份修改
-
views.py
class AdminModifyModelForm(forms.ModelForm): class Meta: model = models.Adminrole fields = ["username", "role"] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for name, field in self.fields.items(): field.widget.attrs = {"class": "form-control", "autocomplete": "off"} # 修改账户和权限 def modify_admin(request, nid): title = "编辑页面" title_obj = models.Adminrole.objects.filter(id=nid).first() if request.method == "GET": form = AdminModifyModelForm(instance=title_obj) return render(request, "admin_role/add_modify.html",{"form":form, "title":title}) form = AdminModifyModelForm(instance=title_obj,data=request.POST) if form.is_valid(): form.save() return redirect("/admin/list/") return render(request, "admin_role/add_modify.html", {"form": form, "title": title})
重置密码
-
views.py
class AdminResetModelForm(forms.ModelForm): # 创建一个确认密码的输入框,然后将input类型修改为password confirm_password = forms.CharField(label="确认密码", widget=forms.PasswordInput) class Meta: model = models.Adminrole fields = ["password"] widgets = { # render_value=True 保留这个字段的值 "password": forms.PasswordInput(render_value=True) } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for name, field in self.fields.items(): field.widget.attrs = {"class": "form-control", "autocomplete": "off"} # 钩子方法,处理改字段 # 将第一次输入的密码进行加密处理 def clean_password(self): # 获取用户框输入的该字段 pwd = self.cleaned_data.get("password") # print(f"第一次获取的password:{pwd}") # print("*-*"*30) return pwd_data.md5(pwd) # 将确认密码也进行同样的加密方式 def clean_confirm_password(self): confirm = pwd_data.md5(self.cleaned_data.get("confirm_password")) # 获取到第一次数据的密码(加密后的) pwd = self.cleaned_data.get("password") # print(f"加密之后的确认密码:{confirm}") # print(f"第二次获取输入的密码:{pwd}") if confirm != pwd: raise ValidationError("两次密码不一致") # 将需要存储的字段返回 return confirm # 重置密码 def reset_admin(request, nid): title_obj = models.Adminrole.objects.filter(id=nid).first() title = f"重置《{title_obj.username}》密码" if request.method == "GET": form = AdminResetModelForm() return render(request, "admin_role/add_modify.html",{"form":form, "title":title}) form = AdminResetModelForm(data=request.POST,instance=title_obj) if form.is_valid(): form.save() return redirect("/admin/list/") return render(request, "admin_role/add_modify.html", {"form": form, "title": title})