Django 中 cookie的使用
Cookie是瀏覽器在客戶端留下的一段記錄,這段記錄可以保留在內(nèi)存或者硬盤上。因?yàn)镠ttp請(qǐng)求是無狀態(tài)的,通過讀取cookie的記錄,服務(wù)器或者客戶端可以維持會(huì)話中的狀態(tài)。比如一個(gè)常見的應(yīng)用場(chǎng)景就是登錄狀態(tài)。Django里面,對(duì)cookie的讀取和設(shè)置很簡(jiǎn)單。Cookie本身的格式類似字典,因此可以通過request的key或者get獲?。蝗缓笏脑O(shè)置則是通過response對(duì)象的set_cookie設(shè)定; 如果要取消cookie,把過期時(shí)間設(shè)置為當(dāng)前時(shí)間就行了。
獲取Cookie:
request.COOKIES['key']
request.get_signed_cookie(key, default=RAISE_ERROR, salt='', max_age=None)
參數(shù):
default: 默認(rèn)值
salt: 加密鹽
max_age: 后臺(tái)控制過期時(shí)間
設(shè)置Cookie:
rep = HttpResponse(...) 或 rep = render(request, ...)
rep.set_cookie(key,value,...)
rep.set_signed_cookie(key,value,salt='加密鹽',...)
參數(shù):
key, 鍵
value='', 值
max_age=None, 超時(shí)時(shí)間
expires=None, 超時(shí)時(shí)間(IE requires expires, so set it if hasn't been already.)
path='/', Cookie生效的路徑,/ 表示根路徑,特殊的:跟路徑的cookie可以被任何url的頁面訪問
domain=None, Cookie生效的域名
secure=False, https傳輸
httponly=False 只能http協(xié)議傳輸,無法被JavaScript獲取(不是絕對(duì),底層抓包可以獲取到也可以被覆蓋)
例1 設(shè)置一個(gè)login登錄界面,一個(gè)index登錄成功之后的跳轉(zhuǎn)界面,如果沒有登錄那么自動(dòng)跳轉(zhuǎn)到登錄界面
views.py
def index(reqeust):
# 獲取當(dāng)前已經(jīng)登錄的用戶
v = reqeust.COOKIES.get('username111')
if not v:
return redirect('/login/')
return render(reqeust,'index.html',{'current_user': v})
注意Cookie的超時(shí)時(shí)間有2種方式,一個(gè)是直接指定max_age(N秒后超時(shí)),一個(gè)是指定expires后面跟一個(gè)具體的時(shí)間對(duì)象
httponly可以禁止JavaScript獲取這個(gè)值,但是實(shí)際上沒有什么鳥用,chrome或者抓包都能輕松獲取所有的cookie
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>歡迎登錄:{{ current_user }}</h1>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="/login/" method="POST">
<input type="text" name="username" placeholder="用戶名" />
<input type="password" name="pwd" placeholder="密碼" />
<input type="submit" />
</form>
</body>
</html>
例2:
現(xiàn)實(shí)生活中,一般是把這個(gè)驗(yàn)證cookie的功能寫成裝飾器,這樣直接在其他函數(shù)上面調(diào)用就行了
把例1改一下
def auth(func):
def inner(reqeust,*args,**kwargs):
v = reqeust.COOKIES.get('username111')
if not v:
return redirect('/login/')
return func(reqeust, *args,**kwargs)
return inner
@auth
def index(reqeust):
# 獲取當(dāng)前已經(jīng)登錄的用戶
v = reqeust.COOKIES.get('username111')
return render(reqeust,'index.html',{'current_user': v})
例3: 我們知道可以使用fbv或者cbv來路由函數(shù)。例2使用了fbv的方式,用cbv也能實(shí)現(xiàn)
cbv里面,如果只打算裝飾一個(gè)方法,那么直接在方法前面加個(gè)@method_decorator就行;如果打算裝飾這個(gè)類里面所有的方法,那么在整個(gè)類的最上面進(jìn)行裝飾
views.py
@method_decorator(auth,name='dispatch')
class Order(views.View):
# @method_decorator(auth)
# def dispatch(self, request, *args, **kwargs):
# return super(Order,self).dispatch(request, *args, **kwargs)
# @method_decorator(auth)
def get(self,reqeust):
v = reqeust.COOKIES.get('username111')
return render(reqeust,'index.html',{'current_user': v})
def post(self,reqeust):
v = reqeust.COOKIES.get('username111')
return render(reqeust,'index.html',{'current_user': v})
urls.py
url(r'^order/', views.Order.as_view()),
例4 我們還可以通過JavaScript或者JQuery來設(shè)置Cookie,比如在前面分頁的代碼基礎(chǔ)上,我們?cè)黾右粋€(gè)自定義顯示行數(shù)的功能。
user_list.html 這里下了一個(gè)JQuery的插件,這樣讀取設(shè)置cookie比較容易;而且,我們還限制了cookie的使用范圍,不是默認(rèn)的所有范圍,而是僅僅局限于/user_list這個(gè)路徑里面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.go{
width:20px;
border: solid 1px;
color: #66512c;
display: inline-block;
padding: 5px;
}
.pagination .page{
border: solid 1px;
color: #66512c;
display: inline-block;
padding: 5px;
background-color: papayawhip;
margin: 5px;
}
.pagination .page.active{
background-color: brown;
color: white;
}
</style>
</head>
<body>
<ul>
{% for item in li %}
{% include 'li.html' %}
{% endfor %}
</ul>
<div>
<select id="ps" onchange="changePageSize(this)">
<option value="10">10</option>
<option value="30">30</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
</div>
<div class="pagination">
{{ page_str }}
</div>
<script src="/static/jquery-1.12.4.js"></script>
<script src="/static/jquery.cookie.js"></script>
<script>
$(function(){
var v = $.cookie('per_page_count', {'path': "/user_list/`"});
console.log(v)
$('#ps').val(v);
});
function changePageSize(ths){
var v = $(ths).val();
console.log(v);
$.cookie('per_page_count',v, {'path': "/user_list/"});
location.reload();
}
</script>
</body>
</html>
views.py 從前端獲取每頁行數(shù),實(shí)例化的時(shí)候傳遞給我們的分頁類
def user_list(request):
current_page = request.GET.get('p', 1)
current_page = int(current_page)
val = request.COOKIES.get('per_page_count',10)
val = int(val)
page_obj = pagination.Page(current_page,len(LIST),val)
data = LIST[page_obj.start:page_obj.end]
page_str = page_obj.page_str("/user_list/")
return render(request, 'user_list.html', {'li': data,'page_str': page_str})
總結(jié)
以上所述是小編給大家介紹的Django 中 cookie的使用,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
laravel 實(shí)現(xiàn)劃分admin和home 模塊分組
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)劃分admin和home 模塊分組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
解析php DOMElement 操作xml 文檔的實(shí)現(xiàn)代碼
本篇文章是對(duì)在php中DOMElement 操作xml 文檔的實(shí)現(xiàn)進(jìn)行了分析介紹。需要的朋友參考下2013-05-05
ThinkPHP中的關(guān)聯(lián)模型注意點(diǎn)
這篇文章主要介紹了初學(xué)ThinkPHP中的關(guān)聯(lián)模型注意點(diǎn),需要的朋友可以參考下2014-06-06
PHP實(shí)現(xiàn)AJAX動(dòng)態(tài)網(wǎng)頁及相關(guān)函數(shù)詳解
ajax其實(shí)是利用javascript向服務(wù)器請(qǐng)求數(shù)據(jù),然后局部修改頁面,下面這篇文章主要給大家介紹了關(guān)于PHP實(shí)現(xiàn)AJAX動(dòng)態(tài)網(wǎng)頁及相關(guān)函數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Laravel使用memcached緩存對(duì)文章增刪改查進(jìn)行優(yōu)化的方法
這篇文章主要介紹了Laravel使用memcached緩存對(duì)文章增刪改查進(jìn)行優(yōu)化的方法,結(jié)合實(shí)例形式分析了Laravel框架中使用memcached緩存實(shí)現(xiàn)針對(duì)增刪改查等操作的優(yōu)化功能,需要的朋友可以參考下2016-10-10

