Django rest-framework框架-CSRF驗證


settings.py里面有一個中間件

django.middleware.csrf.CsrfViewmiddleware  #如果注釋掉全站不需要csrf驗證  如果打開全站都要csrf驗證 全局使用csrf認證

csrf-token是用django中間件來實現的

 

from django.views.decorators.csrf import csrf_exempt,csrf_protect
from django.utils.decorators import method_decorator

#給函數加裝飾器

@csrf_protect #裝飾器 該函數需要csrf token認證
@csrf_exempt #裝飾器 免除csrf token認證
def update_order(request):
      return string

 

#給類里面的方法加裝飾器

1.第一張方式

class Test(View):
  @method_decorator(csrf_exempt)  #給類里面的方法加裝飾器  需要導入一個方法method_decorator
  def get(self, request):
    return HttpResponse("test")

2.第二種方式

@method_decorator(csrf_exempt,name='get')   #找到類里面的get方法加上 裝飾器csrf_exempt
class Test(View):
    def get(self, request):
     return HttpResponse("test")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM