python3 之 天天生鲜 项目(初学者)4
登录验证authenticate 及 登录login 、session状态保持、 退出logout
class LoginView(View): def get(self,request): return render(request,"login.html") def post(self,request): data = request.POST username = data.get("username") password = data.get("pwd") remembered = data.get("remembered") if not all([username,password]): return render(request,"login.html",{"error":"请输入正确的账号或密码"})
#验证用户和密码 user = authenticate(username=username,password=password) if not 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