1.客户列表
当点击后台管理页面的客户管理时
在urls文件中定义该请求地址
path('admin/user_list/',viewsAdmin.user_list),
在viewAdmin文件中创建处理该请求的函数
# 处理客户管理请求
def user_list(request):
# 读取分页页码
try:
ym = request.GET["ym"]
except:
ym = 1
# 查询所有用户信息
users = User.objects.all().order_by("id")
# 将该分类的商品信息进行分页处理,每页显示5条记录
pag = paginator.Paginator(users, 5)
# 根据当前页码获取当前分页信息
pageInfo = pag.get_page(ym)
# 获取当前页的商品列表信息
orders = pageInfo.object_list
# 获取总页码数
yms = pag.page_range
return render(request, "adminTemp/user_list.html",
{"users": users, "page": pageInfo, "yms": yms})
<!DOCTYPE html>
<html>
<head>
<title>客户列表</title>
{% load static %}
<meta charset="utf-8"/>
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
<style>
#nav{
width: 100%;
height: 40px;
margin-top: 20px;
clear: left;
}
#nav a{
text-decoration: none;
margin: 3px;
background-color: cadetblue;
}
#nav a:hover{
background-color: #FF1719;
}
#nav a.c_page{
background-color: #FF1719;
}
#nav a.up_page,#nav a.do_page{
width: 70px;
height: 30px;
padding: 5px 10px;
border-radius: 5px;
color: white;
display: inline-block;
}
#nav a.p_page{
width: 30px;
height: 30px;
padding: 5px;
border-radius: 5px;
color: white;
display: inline-block;
}
</style>
</head>
<body>
<div class="container-fluid">
{% include "adminTemp/header.html" %}
<div class="text-right"><a class="btn btn-warning" href="/admin/user_add/">添加客户</a></div>
{% if msg %}
<div class="alert alert-success">{{ msg }}</div>
{% endif %}
{% if failMsg %}
<div class="alert alert-success">{{ failMsg }}</div>
{% endif %}
<br>
<br>
<table class="table table-bordered table-hover">
<tr>
<th width="5%">ID</th>
<th width="10%">用户名</th>
<th width="10%">邮箱</th>
<th width="10%">收件人</th>
<th width="10%">电话</th>
<th width="10%">地址</th>
<th width="12%">操作</th>
</tr>
{% for u in users %}
<tr>
<td><p>{{ u.id }}</p></td>
<td><p>{{ u.username }}</p></td>
<td><p>{{ u.email }}</p></td>
<td><p>{{ u.name }}</p></td>
<td><p>{{ u.phone }}</p></td>
<td><p>{{ u.address }}</p></td>
<td>
<a class="btn btn-info"
href="/admin/user_reset/?id={{ u.id }}&username={{ u.username }}&email={{ u.email }}">重置密码</a>
<a class="btn btn-primary" href="/admin/user_editshow/?id={{ u.id }}">修改</a>
<a class="btn btn-danger" href="/admin/user_delete/?id={{ u.id }}">删除</a>
</td>
</tr>
{% endfor %}
</table>
<br>
<!-- 显示页码导航栏 -->
<div id="nav" align="center">
<!-- 上一页 -->
<!-- 判断当前页是否有上一页,如果有上一页则显示上一页的按钮,否则就不显示上一页 -->
{% if page.has_previous %}
<a href="/admin/user_list/?ym={{ page.previous_page_number }}" class="up_page">上一页</a>
{% endif %}
<!-- 页码 -->
{% for ym in yms %}
{% if page.number == ym %}
<a href="/admin/user_list/?ym={{ ym }}" class="p_page c_page">{{ ym }}</a>
{% else %}
<a href="/admin/user_list/?ym={{ ym }}" class="p_page">{{ ym }}</a>
{% endif %}
{% endfor %}
<!-- 下一页 -->
{% if page.has_next %}
<a href="/admin/user_list/?ym={{ page.next_page_number }}" class="do_page">下一页</a>
{% endif %}
</div>
<br>
</div>
</body>
</html>
2.添加客户
点击上图中的添加客户按钮,触发事件给服务器发送请求
在urls文件中定义该请求地址
path('admin/user_add/',viewsAdmin.user_add),
在viewAdmin文件中创建处理函数
# 处理添加用户的页面跳转
def user_add(request):
return render(request, "adminTemp/user_add.html")
<!DOCTYPE html>
<html>
<head>
<title>客户添加</title>
{% load static %}
<meta charset="utf-8"/>
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
</head>
<body>
<div class="container-fluid">
{% include "adminTemp/header.html" %}
{% if failMsg %}
<div class="alert alert-danger">{{ failMsg }}</div>
{% endif %}
<br><br>
<form class="form-horizontal" action="/admin/add/" method="post">
{% csrf_token %}
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">用户名</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="username" required="required"
value="{{ u.username }}"/>
</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">邮箱</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="email" required="required"
value="{{ u.email }}"/>
</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">密码</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="password" required="required"
value="{{ u.password }}"/>
</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">收货人</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="name" value="{{ u.name }}"/>
</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">电话</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="phone" value="{{ u.phone }}"/>
</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">地址</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="address" value="{{ u.address }}"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<button type="submit" class="btn btn-success">提交保存</button>
</div>
</div>
</form>
<span style="color:red;"></span>
</div>
</body>
</html>
当管理员填写新客户信息后哦,点击提交保存,提交表单信息至服务器
在urls文件中定义接收表单信息的请求地址
path('admin/add/',viewsAdmin.add),
在viewsAdmin文件中创建函数
# 处理添加用户的请求
def add(request):
# 用户名
username = request.POST["username"]
# 邮箱
email = request.POST["email"]
# 密码
password = request.POST["password"]
# 收件人
name = request.POST["name"]
# 电话
phone = request.POST["phone"]
# 地址
address = request.POST["address"]
# 添加新用户
user = User.objects.create(username=username, email=email, password=password, name=name, phone=phone, address=address)
if user:
return redirect(user_list)
else:
key = "failMsg"
value = "用户名或邮箱重复,请重新填写!"
return render(request, "adminTemp/user_add.html", {"u": user, key: value})
3.重置密码
在客户管理列表页面中,选择一个用户进行重置,点击重置密码按钮触发请求
path('admin/user_reset/',viewsAdmin.user_reset),
# 处理重置密码请求
def user_reset(request):
id = request.GET["id"]
username = request.GET["username"]
email = request.GET["email"]
return render(request, "adminTemp/user_reset.html", {"id": id, "username": username, "email": email})
<!DOCTYPE html>
<html>
<head>
<title>重置密码</title>
{% load static %}
<meta charset="utf-8"/>
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
</head>
<body>
<div class="container-fluid">
{% include "adminTemp/header.html" %}
<br><br>
<form class="form-horizontal" action="/admin/reset/" method="post">
{% csrf_token %}
<input type="hidden" name="id" value="{{ id }}">
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">用户名</label>
<div class="col-sm-5">{{ username }}</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">邮箱</label>
<div class="col-sm-5">{{ email }}</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">密码</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="password" value="" required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<button type="submit" class="btn btn-success">提交修改</button>
</div>
</div>
</form>
<span style="color:red;"></span>
</div>
</body>
</html>
当管理员重置后,点击提交修改按钮发送请求
path('admin/reset/',viewsAdmin.reset),
# 处理密码重置
def reset(request):
id = request.POST["id"]
password = request.POST["password"]
# 修改密码
user = User.objects.filter(id=id)
user.update(password=password)
return redirect(user_list)
4.修改客户信息
管理员选择一个客户信息进行修改时,点击修改按钮
path('admin/user_editshow/',viewsAdmin.user_editshow),
# 处理修改页面的请求
def user_editshow(request):
id = request.GET["id"]
user = User.objects.get(id=id)
return render(request, "adminTemp/user_edit.html", {"u": user})
<!DOCTYPE html>
<html>
<head>
<title>客户修改</title>
{% load static %}
<meta charset="utf-8"/>
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}"/>
</head>
<body>
<div class="container-fluid">
{% include "adminTemp/header.html" %}
<br><br>
<form class="form-horizontal" action="/admin/user_edit/" method="post">
{% csrf_token %}
<input type="hidden" name="id" value="{{ u.id }}">
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">用户名</label>
<div class="col-sm-5">{{ u.username }}</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">邮箱</label>
<div class="col-sm-5">{{ u.email }}</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">收货人</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="name" value="{{ u.name }}">
</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">电话</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="phone" value="{{ u.phone }}">
</div>
</div>
<div class="form-group">
<label for="input_name" class="col-sm-1 control-label">地址</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="input_name" name="address" value="{{ u.address }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<button type="submit" class="btn btn-success">提交修改</button>
</div>
</div>
</form>
<span style="color:red;"></span>
</div>
</body>
</html>
当管理员修改客户信息后,点击提交修改按钮,提交修改后的数据
path('admin/user_edit/',viewsAdmin.user_edit),
# 处理修改请求
def user_edit(request):
id = request.POST["id"]
name = request.POST["name"]
phone = request.POST["phone"]
address = request.POST["address"]
user = User.objects.filter(id=id)
user.update(name=name, phone=phone, address=address)
return redirect(user_list)
5.删除客户
当管理员选择某个客户进行删除时,点击删除按钮
path('admin/user_delete/',viewsAdmin.user_delete),
# 处理删除用户请求
def user_delete(request):
id = request.GET["id"]
user = User.objects.get(id=id)
user.delete()
return redirect(user_list)