python3之天天生鲜项目(初学者)4
登录验证authenticate 及 登录login 、session状态保持、 退出logout
class LoginView(View):def get(self,request):return render(request,"login.html")def post(self,request):data
= request.POSTusername
= data.get("username")password
= data.get("pwd")remembered
= data.get("remembered")ifnot all([username,password]):return render(request,"login.html",{"error":"请输入正确的账号或密码"})#验证用户和密码
user
= authenticate(username=username,password=password)ifnot user:return render(request, "login.html", {"error": "请输入正确的账号或密码"})#是否激活if user.is_active == False:
return render(request, "login.html", {"error": "邮箱未激活"})
#登录login(request,user)
#记住用户名 session存储位置 mysql--...-- django_session里
if remembered != "on":
request.session.set_expiry(0)
else:
request.session.set_expiry(3600*24)
response = redirect("/goods/index")
return response
#退出登陆 清空sessionclass LogoutUser(View):
def get(self,request):
logout(request)
return redirect("/goods/index")
#状态保持 设置为0时 关闭浏览器就会退出登录 但不清空session 退出则清空 if remembered != "on":
request.session.set_expiry(0)
else:
request.session.set_expiry(3600*24)
logout退出后 清空当前用户的状态
以上是 python3之天天生鲜项目(初学者)4 的全部内容, 来源链接: utcz.com/z/529867.html