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

相关文章:

  • 2021-09-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-04
  • 2021-11-30
  • 2021-07-31
  • 2022-12-23
相关资源
相似解决方案