标签:判断 code 直接 ext text end log efault 访问
cookie作用根据当前浏览器的cookie值,判断用户是否已经有效登陆到系统,获取访问权。
当用户跳过登陆页面,直接访问home,直接返回到login.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登陆页面</h1>
<form action="/ormtest/login/" method="post">
<p>
用户名:<input type="text" name="user">
</p>
<p>
密码:<input type="password" name="pwd">
</p>
<p>
<input type="submit" value="提交">
</p>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是首页啊</title>
</head>
<body>
<h1>我是首页啊</h1>
</body>
</html>
def login(request):
if request.method == "GET":
return render(request,"login.html")
else:
user = request.POST.get("user")
pwd = request.POST.get("pwd")
if user == "kang" and pwd == "123456":
rep = redirect("/ormtest/home/")
#设置一个加密的cookie,默认值为"1",加密条件"MingKangZhou"
rep.set_signed_cookie("is_login","1",salt="MingKangZhou")
return rep
else:
return render(request, "login.html")
def home(request):
#获取当前浏览器"is_login"的cookie值
ret = request.get_signed_cookie("is_login",default="0",salt="MingKangZhou")
if ret == "1": #如果为1,代表已经登陆成功
return render(request,"home.html")
else: #如果不为"1",即返回到登陆页面
return redirect("/ormtest/login/")
标签:判断 code 直接 ext text end log efault 访问
原文地址:https://blog.51cto.com/12965094/2396300