django-csrf使用和禁用方式
orm表單使用csrf
a. 基本應(yīng)用
form表單中添加
{% csrf_token %}
b. 全站禁用
# ’django.middleware.csrf.CsrfViewMiddleware’,
c. 局部禁用
’django.middleware.csrf.CsrfViewMiddleware’, from django.views.decorators.csrf import csrf_exempt @csrf_exempt def csrf1(request): if request.method == ’GET’: return render(request,’csrf1.html’) else: return HttpResponse(’ok’)
d. 局部使用
# ’django.middleware.csrf.CsrfViewMiddleware’, from django.views.decorators.csrf import csrf_exempt,csrf_protect @csrf_protect def csrf1(request): if request.method == ’GET’: return render(request,’csrf1.html’) else: return HttpResponse(’ok’)
ajax提交數(shù)據(jù)
Ajax提交數(shù)據(jù)時(shí)候,攜帶CSRF:
a. 放置在data中攜帶
<form method='POST' action='/csrf1.html'> {% csrf_token %} <input type='text' name='user' /> <input type='submit' value='提交'/> <a onclick='submitForm();'>Ajax提交</a></form><script src='https://rkxy.com.cn/static/jquery-1.12.4.js'></script><script> function submitForm(){ var csrf = $(’input[name='csrfmiddlewaretoken']’).val(); var user = $(’#user’).val(); $.ajax({ url: ’/csrf1.html’, type: ’POST’, data: { 'user':user,’csrfmiddlewaretoken’: csrf}, success:function(arg){console.log(arg); } }) }</script>
b. 放在請(qǐng)求頭中
<form method='POST' action='/csrf1.html'> {% csrf_token %} <input type='text' name='user' /> <input type='submit' value='提交'/> <a onclick='submitForm();'>Ajax提交</a></form><script src='https://rkxy.com.cn/static/jquery-1.12.4.js'></script><script src='https://rkxy.com.cn/static/jquery.cookie.js'></script><script> function submitForm(){ var token = $.cookie(’csrftoken’); var user = $(’#user’).val(); $.ajax({ url: ’/csrf1.html’, type: ’POST’, headers:{’X-CSRFToken’: token}, data: { 'user':user}, success:function(arg){console.log(arg); } }) }</script>
補(bǔ)充知識(shí):在django的form表單及ajax提交的數(shù)據(jù)中添加認(rèn)證的csrfmiddlewaretoken
1. 對(duì)于ajax提交數(shù)據(jù),把下面的代碼加入到j(luò)s的頭部,可以保證ajax執(zhí)行時(shí)自動(dòng)提交參數(shù)csrfmiddlewaretoken。
$.ajaxSetup({data: {csrfmiddlewaretoken: ’{{ csrf_token }}’ }});
2. 對(duì)于form表單提交數(shù)據(jù),在表單內(nèi)部加入{% csrf_token %}標(biāo)簽,會(huì)自動(dòng)生成一個(gè)input標(biāo)簽
<form>{% csrf_token %}</form>
得到
<form><input name='csrfmiddlewaretoken' value='...' type='hidden'></form>
或者使用js添加:
$('#csrf_token').replaceWith('{% csrf_token %}');
3. 另外記錄一下使用模板過濾器處理form.field的方法
$('#{{ field.name }}').replaceWith(’{{ field|linebreaksbr }}’);$('.{{ field.name }}').text(’{{ field.errors|striptags }}’);{{ value|linebreaksbr }}: 'Joelnis a slug' => 'Joel<br>is a slug'{{ value|striptags }}: '<b>Joel</b> <button>is</button> a <span>slug</span>' => 'Joel is a slug'.
以上這篇django-csrf使用和禁用方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 如何在 Matplotlib 中繪制垂直線2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. ASP常用日期格式化函數(shù) FormatDate()4. python中@contextmanager實(shí)例用法5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 如何通過python實(shí)現(xiàn)IOU計(jì)算代碼實(shí)例9. 開發(fā)效率翻倍的Web API使用技巧10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
