目錄html
就相似於你搭建了一個跟銀行如出一轍的web頁面
用戶在你的網站轉帳的時候輸入用戶名 密碼 對方帳戶
銀行裏面的錢確實少了 可是發現收款人變了前端
前端python
<h1>真正的網站</h1> <form action="" method="post"> <p>username:<input type="text" name="username"></p> <p>target_username:<input type="text" name="target_username"> </p> <p>money:<input type="text" name="money"></p> <input type="submit"> </form>
後端web
def transfer(request):#轉帳 if request.method=="POST": username = request.POST.get('username') target_username = request.POST.get('target_username') money = request.POST.get('money') print('%s給%s轉了%s錢'%(username,target_username,money)) return render(request,'formm.html')
前端ajax
<h1>釣魚的網站</h1> <form action="http://127.0.0.1:8000/transfer/" method="post"> <p>username:<input type="text" name="username"></p> <input type="text" name="target_username" value="sky" style="display: none"> <p>target_username:<input type="text"></p> <p>money:<input type="text" name="money"></p> <input type="submit"> </form>
後端django
def transfer(request): return render(request,'taansfer.html')
{% csrf_token %} 給出一個隨機字符串,用來進行校驗後端
<h1>真正的網站</h1> <form action="" method="post"> {% csrf_token %} <p>username:<input type="text" name="username"></p> <p>target_username:<input type="text" name="target_username"> </p> <p>money:<input type="text" name="money"></p> <input type="submit"> </form>
第一種 本身再頁面上先經過{% csrf_token %}獲取到隨機字符串 而後利用標籤查找cookie
data{'username':'jason','csrfmiddlewaretoken':$('[name="csrfmiddlewaretoken"]').val()},
post
<h1>真正的網站</h1> <form action="" method="post"> {% csrf_token %} <p>username:<input type="text" name="username"></p> <p>target_username:<input type="text" name="target_username"></p> <p>money:<input type="text" name="money"></p> </form> <button id="b1">發ajax</button> <script> $('#b1').click(function () { $.ajax({ url: "", type: "post", data: {"username": "sky", "csrfmiddlewaretoken": $('[name="csrfmiddlewaretoken"]').val()}, success: function (data) { alert(data) } }) }) </script>
第二種data:{'username':'jason','csrfmiddlewaretoken':'{{ csrf_token }}'},
網站
<h1>真正的網站</h1> <form action="" method="post"> {% csrf_token %} <p>username:<input type="text" name="username"></p> <p>target_username:<input type="text" name="target_username"></p> <p>money:<input type="text" name="money"></p> </form> <button id="b1">發ajax</button> <script> $('#b1').click(function () { $.ajax({ url: "", type: "post", data: {"username": "sky", "csrfmiddlewaretoken": "{{csrf_token }}"}, success: function (data) { alert(data) } }) }) </script>
前端,導入下面的js
<h1>真正的網站</h1> <form action="" method="post"> {% csrf_token %} <p>username:<input type="text" name="username"></p> <p>target_username:<input type="text" name="target_username"></p> <p>money:<input type="text" name="money"></p> </form> <button id="b1">發ajax</button> <script src="/static/setpe.js"></script> <script> $('#b1').click(function () { $.ajax({ url: "", type: "post", data: {"username": "sky"}, success: function (data) { alert(data) } }) }) </script>
第三種
拷貝下面js文件,新建文件夾static,而後靜態文件配置,而後導入前端文件
js文件
function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie !== '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } var csrftoken = getCookie('csrftoken'); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } $.ajaxSetup({ beforeSend: function (xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } });
將上面的django的靜態文件中,在html頁面上經過導入該文件便可自動幫咱們解決ajax提交post數據時校驗csrf_token的問題,(導入該配置文件以前,須要先導入jQuery,由於這個配置文件內的內容是基於jQuery來實現的)
更多細節詳見:Djagno官方文檔中關於CSRF的內容
csrf_exempt 只有兩種裝飾的方式
from django.views.decorators.csrf import csrf_exempt, csrf_protect #csrf_exempt 不校驗 # csrf_protect 校驗
from django.views.decorators.csrf import csrf_exempt, csrf_protect from django.utils.decorators import method_decorator @csrf_exempt def exem(request): return HttpResponse('exempt') @csrf_protect def pro(request): return HttpResponse('pro')
除了csrf_exempt以外 全部的其餘裝飾器 在CBV上面都有三種方式
命名@method_decorator(csrf_exempt,name='dispatch')
from django.views.decorators.csrf import csrf_exempt, csrf_protect from django.utils.decorators import method_decorator from django.views import View # 第一種 # @method_decorator(csrf_exempt,name='dispatch') class MyCsrf(View): @method_decorator(csrf_protect) def dispatch(self, request, *args, **kwargs): return super().dispatch(request,*args,**kwargs) def get(self,request): return HttpResponse('hahaha') @method_decorator(csrf_protect) def post(self,request): return HttpResponse('post')
@method_decorator(csrf_protect)
from django.views.decorators.csrf import csrf_exempt, csrf_protect from django.utils.decorators import method_decorator from django.views import View class MyCsrf(View): @method_decorator(csrf_protect) def dispatch(self, request, *args, **kwargs): return super().dispatch(request,*args,**kwargs) def get(self,request): return HttpResponse('hahaha') @method_decorator(csrf_protect) def post(self,request): return HttpResponse('post')