概念
通过以上多篇文章的讲解,对该项目的功能已经实现了很多,本文将对该项目的用户注册,登录,修改用户信息,以及退出等功能的实现。
注册功能实现
点击head.html头部页面的注册按钮,触发超链接跳转至user_register.html页面,其页面代码如下
在urls.py文件中定义该请求地址
python
path('register/',views.register),
在views.py文件中创建处理该请求的函数
python
# 跳转至注册页面
def register(request):
return render(request,"user_register.html",{"typeList": types})
其注册页面代码如下
html
<!DOCTYPE html>
<html>
<head>
<title>用户注册</title>
{% load static %}
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
<link type="text/css" rel="stylesheet" href="{% static 'css/style.css' %}">
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/simpleCart.min.js' %}"></script>
</head>
<body>
{% include "header.html" with flag=10 typeList=typeList %}
<div class="account">
<div class="container">
<div class="register">
{% if msg %}
<div class="alert alert-danger">{{ msg }}</div>
{% endif %}
<form action="/user_rigister/" method="post">
{% csrf_token %}
<div class="register-top-grid">
<h3>注册新用户</h3>
<div class="input">
<span>用户名 <label style="color:red;">*</label></span>
<input type="text" name="username" placeholder="请输入用户名" required="required">
</div>
<div class="input">
<span>邮箱 <label style="color:red;">*</label></span>
<input type="text" name="email" placeholder="请输入邮箱" required="required">
</div>
<div class="input">
<span>密码 <label style="color:red;">*</label></span>
<input type="password" name="password" placeholder="请输入密码" required="required">
</div>
<div class="input">
<span>收货人<label></label></span>
<input type="text" name="name" placeholder="请输入收货人姓名">
</div>
<div class="input">
<span>收货电话<label></label></span>
<input type="text" name="phone" placeholder="请输入收货电话">
</div>
<div class="input">
<span>收货地址<label></label></span>
<input type="text" name="address" placeholder="请输入收货地址">
</div>
<div class="clearfix"> </div>
</div>
<div class="register-but text-center">
<input type="submit" value="提交">
<div class="clearfix"> </div>
</div>
</form>
<div class="clearfix"> </div>
</div>
</div>
</div>
{% include "footer.html" %}
</body>
</html>
用户填写注册信息后,点击提交按钮,将信息发送给后台服务器,在urls.py文件中定义请求地址
python
path('user_rigister/',views.user_rigister),
在views.py文件中创建处理该函数
python
# 处理注册请求
def user_rigister(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"]
# 判断用户名是否存在
users=User.objects.filter(username=username)
# 判断邮箱是否存在
emails=User.objects.filter(email=email)
if users or emails:
msg="用户名或邮箱重复,请重新填写!"
path="user_register.html"
else:
User.objects.create(username=username,email=email,password=password,name=name,phone=phone,address=address,isadmin=False,isvalidate=False)
msg = "注册成功,请登录!"
path = "user_login.html"
return render(request,path,{"msg":msg,"typeList": types})
当注册成功,跳转至登录页面,页面代码如下,如果注册失败,则会重新刷新注册页面
html
<!DOCTYPE html>
<html>
<head>
<title>用户登录</title>
{% load static %}
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
<link type="text/css" rel="stylesheet" href="{% static 'css/style.css' %}">
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/simpleCart.min.js' %}"></script>
</head>
<body>
{% include "header.html" with flag=9 typeList=typeList %}
<div class="account">
<div class="container">
<div class="register">
{% if msg %}
<div class="alert alert-success">{{ msg }}</div>
{% endif %}
{% if failMsg %}
<div class="alert alert-danger">{{ failMsg }}</div>
{% endif %}
<form action="/user_login" method="post">
{% csrf_token %}
<div class="register-top-grid">
<h3>用户登录</h3>
<div class="input">
<span>用户名/邮箱 <label style="color:red;">*</label></span>
<input type="text" name="ue" placeholder="请输入用户名" required="required">
</div>
<div class="input">
<span>密码 <label style="color:red;">*</label></span>
<input type="password" name="password" placeholder="请输入密码" required="required">
</div>
<div class="clearfix"> </div>
</div>
<div class="register-but text-center">
<input type="submit" value="提交">
<div class="clearfix"> </div>
</div>
</form>
<div class="clearfix"> </div>
</div>
</div>
</div>
{% include "footer.html" %}
</body>
</html>
登录功能实现
当注册成功后,进入登录页面,界面如上图所示,也可以点击登录选项进入登录页面
在urls.py文件中定义该请求地址
python
path('login/',views.login),
在views文件中创建该函数
python
# 处理跳转登录页面的请求
def login(request):
return render(request, "user_login.html", {"typeList": types})
当用户输入用户名和密码后,点击提交,将用户登录信息发送给后台服务器
在urls文件中定义请求地址
python
path('user_login/',views.user_login),
在setting.py文件中,添加以下代码,使session在固定时间内失效
python
# 设置session过期时间为1小时(3600秒)
SESSION_COOKIE_AGE = 60
在views文件中创建函数处理该请求
python
# 处理登录的请求
def user_login(request):
# 用户名/邮箱
ue=request.POST["ue"]
# 密码
password=request.POST["password"]
# 使用用户名和密码查询该用户信息
users=User.objects.filter(username=ue,password=password)
if not users:
users=User.objects.filter(email=ue,password=password)
# global user
if users:
request.session["isLogin"]=True
u=users.values()[0]
print(u)
request.session["user"]=u
failMsg = ""
path="user_center.html"
else:
user=None
request.session["user"] = user
failMsg="用户名、邮箱或者密码错误,请重新登录!"
path="user_login.html"
print(users.values_list())
return render(request,path,{"typeList": types,"failMsg":failMsg})
当登录成功后,跳转至个人中心页面,页面代码如下
html
<!DOCTYPE html>
<html>
<head>
<title>个人中心</title>
{% load static %}
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
<link type="text/css" rel="stylesheet" href="{% static 'css/style.css' %}">
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/simpleCart.min.js' %}"></script>
</head>
<body>
{% include "header.html" with flag=4 typeList=typeList user=user %}
{% if not user %}
<script>
//当用户没有登录,直接进入个人中心时,强制跳转至未登录的首页页面
location.href="/"
</script>
{% endif %}
<!--account-->
<div class="account">
<div class="container">
<div class="register">
{% if msg %}
<div class="alert alert-success">{{ msg }}</div>
{% endif %}
{% if failMsg %}
<div class="alert alert-danger">{{ failMsg }}</div>
{% endif %}
<div class="register-top-grid">
<h3>个人中心</h3>
<form action="/user_changeaddress/" method="post">
{% csrf_token %}
<!-- 收货信息 start -->
<h4>收货信息</h4>
<div class="input">
<span>收货人<label></label></span>
<input type="text" name="name" value="{{ request.session.user.name }}" placeholder="请输入收货">
</div>
<div class="input">
<span>收货电话</span>
<input type="text" name="phone" value="{{ request.session.user.phone }}" placeholder="请输入收货电话">
</div>
<div class="input">
<span>收货地址</span>
<input type="text" name="address" value="{{ request.session.user.address }}" placeholder="请输入收货地址">
</div>
<div class="register-but text-center">
<input type="submit" value="提交">
</div>
<!-- 收货信息 end -->
</form>
<hr>
<form action="/user_changepwd/" method="post">
{% csrf_token %}
<h4>安全信息</h4>
<div class="input">
<span>原密码</span>
<input type="text" name="password" placeholder="请输入原密码">
</div>
<div class="input">
<span>新密码</span>
<input type="text" name="newPassword" placeholder="请输入新密码">
</div>
<div class="clearfix"> </div>
<div class="register-but text-center">
<input type="submit" value="提交">
</div>
</form>
</div>
<div class="clearfix"> </div>
</div>
</div>
</div>
{% include "footer.html" %}
</body>
</html>
修改用户信息功能
在header.html头部页面中,添加以下超链接
在urls.py文件中定义接收该请求的地址
python
path('user_center/',views.user_center),
在views.py文件中定义该函数处理请求
python
# 个人中心页面
def user_center(request):
return render(request,"user_center.html",{"typeList": types})
1.修改个人收货地址
该页面中,有两个表单,第一个表单用于修改用户个人信息,第二个表单用于修改密码。
在urls文件中添加接收请求地址
python
path('user_changeaddress/',views.user_changeaddress),
在views文件中定义函数处理该请求
python
# 修改个人收件信息
def user_changeaddress(request):
name=request.POST["name"]
phone=request.POST["phone"]
address=request.POST["address"]
user=request.session.get("user")
print(user)
u=User.objects.filter(id=user["id"])
u.update(name=name,phone=phone,address=address)
u = User.objects.filter(id=user["id"]).values()
# 将更新后的数据同步session中
request.session["user"]=u[0]
msg="收件信息更新成功!"
return render(request,"user_center.html",{"typeList": types,"msg":msg})
2.修改密码
在urls文件中定义接收修改密码的请求
python
path('user_changepwd/',views.user_changepwd),
在views文件中创建函数处理修改密码请求
python
# 修改密码
def user_changepwd(request):
psd=request.POST["password"]
newPsd=request.POST["newPassword"]
user = request.session.get("user")
print(user)
key=""
if psd == user["password"]:
u = User.objects.filter(id=user["id"])
u.update(password=newPsd)
key="msg"
msg="修改成功!"
u = User.objects.filter(id=user["id"]).values()
# 将更新后的数据同步session中
request.session["user"] = u[0]
else:
key="failMsg"
msg="修改失败,原密码不正确,你再想想!"
return render(request, "user_center.html", {"typeList": types, key: msg})
退出功能实现
在header.html头部页面中,添加以下超链接
在urls.py文件中定义接收该请求的地址
python
path('user_logout/',views.user_logout),
在views.py文件中定义该函数处理请求
python
#退出登录
def user_logout(request):
try:
del request.session["isLogin"]
del request.session["user"]
except:
print("在线时间超时,请重新登录")
return redirect(index)