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")

猜你喜欢

转载自www.cnblogs.com/kuku0223/p/11327023.html
今日推荐