新聞中心
怎么在django中實(shí)現(xiàn)csrf?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
如果是ajax提交,可以按照下面的方式處理
可以設(shè)置一個(gè)全局的設(shè)置,然后在$(function){
}中執(zhí)行函數(shù)
$(function () { ajax_buttion() $.ajaxSetup() })
如果是form表單提交,則可以按照下面的方式處理
然后返回使用render的方式返回
def test(request): # int("hahah") # print(settings.C) print("test------->views",time.time()) print(request.method) print("_".center(100,"-")) print(request) # return HttpResponse("last_app1") return render(request,"test.html")
中間件里csrf默認(rèn)是全局都生效的,但是如果我們有需求,比如全局生效,但是我某個(gè)函數(shù)不需要使用csrf該怎么辦;或者我的全局不設(shè)置csrf,但是對某個(gè)視圖函數(shù)需要采用csrf,該怎么辦
這里就需要導(dǎo)入2個(gè)模塊
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_protect
然后在視圖函數(shù)中使用使用裝飾器來裝飾視圖函數(shù)
下面的例子就是起到全局啟動(dòng)csrf,但是我這個(gè)函數(shù)不啟動(dòng)csrf
@csrf_exempt def test(request): # int("hahah") # print(settings.C) print("test------->views",time.time()) print(request.method) print("_".center(100,"-")) print(request) # return HttpResponse("last_app1") return render(request,"test.html")
下面的例子就是全局不啟用csrf,但是我這個(gè)函數(shù)不啟動(dòng)csrf
@csrf_protect def test(request): # int("hahah") # print(settings.C) print("test------->views",time.time()) print(request.method) print("_".center(100,"-")) print(request) # return HttpResponse("last_app1") return render(request,"test.html")
關(guān)于怎么在django中實(shí)現(xiàn)csrf問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
網(wǎng)站標(biāo)題:怎么在django中實(shí)現(xiàn)csrf-創(chuàng)新互聯(lián)
路徑分享:http://fisionsoft.com.cn/article/dejdjj.html