根据当前浏览器的cookie值,判断用户是否已经有效登陆到系统,获取访问权。
当用户跳过登陆页面,直接访问home,直接返回到login.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>登陆页面</h2>
<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>
<h2>我是首页啊</h2>
</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/")
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。