Python Django Cookie 簡(jiǎn)單用法解析
home.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>個(gè)人信息頁(yè)面</title> </head> <body> <p>個(gè)人信息頁(yè)面</p> </body> </html>
只有返回一串字符串
login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄頁(yè)面</title>
</head>
<body>
<p>登錄頁(yè)面</p>
<form action="/login/" method="post">
{% csrf_token %}
<p>
賬號(hào):
<input type="text" name="user">
</p>
<p>
密碼:
<input type="text" name="pwd">
</p>
<p>
<input type="submit" value="登錄">
</p>
</form>
</body>
</html>
要考慮加上 csrf_token,不然會(huì) 403

login 函數(shù):
from django.shortcuts import render, redirect
from app01 import models
def login(request):
if request.method == "POST":
username = request.POST.get("user")
password = request.POST.get("pwd")
if username == "admin" and password == "admin":
rep = redirect("/home/") # 得到一個(gè)響應(yīng)對(duì)象
rep.set_cookie("login", "success") # 設(shè)置 cookie
return rep
return render(request, "login.html")
set_cookie() 中的第一個(gè)參數(shù)為 key,第二個(gè)參數(shù)為 value
home 函數(shù):
from django.shortcuts import render, redirect
from app01 import models
def home(request):
ret = request.COOKIES.get("login") # 獲取 cookie 的 value
if ret == "success":
# cookie 驗(yàn)證成功
return render(request, "home.html")
else:
return redirect("/login/")
輸入賬號(hào)、密碼:admin,cookie 驗(yàn)證成功

給 cookie 加鹽:
login 函數(shù):
from django.shortcuts import render, redirect
from app01 import models
def login(request):
if request.method == "POST":
username = request.POST.get("user")
password = request.POST.get("pwd")
if username == "admin" and password == "admin":
rep = redirect("/home/") # 得到一個(gè)響應(yīng)對(duì)象
# rep.set_cookie("login", "success") # 設(shè)置 cookie
rep.set_signed_cookie("login", "success", salt="whoami") # 設(shè)置 cookie 并加鹽
return rep
return render(request, "login.html")
home 函數(shù):
from django.shortcuts import render, redirect
from app01 import models
def home(request):
# ret = request.COOKIES.get("login") # 獲取 cookie 的 value
ret = request.get_signed_cookie("login", salt="whoami") # 獲取加鹽后 cookie 的 value
if ret == "success":
# cookie 驗(yàn)證成功
return render(request, "home.html")
else:
return redirect("/login/")
輸入賬號(hào)、密碼:admin,cookie 驗(yàn)證成功

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jupyter notebook出現(xiàn)In[*]的問(wèn)題及解決
這篇文章主要介紹了jupyter notebook出現(xiàn)In[*]的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Pycharm設(shè)置utf-8自動(dòng)顯示方法
今天小編就為大家分享一篇Pycharm設(shè)置utf-8自動(dòng)顯示方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python中format()函數(shù)的簡(jiǎn)單使用教程
python中format函數(shù)用于字符串的格式化,接下來(lái)通過(guò)本文給大家介紹python中format()函數(shù)的簡(jiǎn)單使用教程,一起看看吧2018-03-03
face++與python實(shí)現(xiàn)人臉識(shí)別簽到(考勤)功能
這篇文章主要為大家詳細(xì)介紹了face++與python實(shí)現(xiàn)人臉識(shí)別簽到(考勤)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
基于Python實(shí)現(xiàn)剪切板實(shí)時(shí)監(jiān)控方法解析
這篇文章主要介紹了基于Python實(shí)現(xiàn)剪切板實(shí)時(shí)監(jiān)控方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

