@csrf_exempt不适用于基于通用视图的类

cas*_*iel 52 python django

class ChromeLoginView(View):

     def get(self, request):
          return JsonResponse({'status': request.user.is_authenticated()})

     @method_decorator(csrf_exempt)
     def post(self, request):
          username = request.POST['username']
          password = request.POST['password']
          user = authenticate(username=username, password=password)
          if user is not None:
                if user.is_active:
                     login(request, user)
                     return JsonResponse({'status': True})
          return JsonResponse({'status': False})
Run Code Online (Sandbox Code Playgroud)

我期待帖子确实由csrf停止,但它返回403错误.

但是如果删除那个装饰器并在URLConf中执行此操作

url(r'^chrome_login/', csrf_exempt(ChromeLoginView.as_view()), name='chrome_login'),
Run Code Online (Sandbox Code Playgroud)

它会工作.

这里发生了什么?它不应该工作,因为我猜这就是method_decorator所做的.我正在使用python3.4和django1.7.1

任何建议都会很棒.

knb*_*nbk 76

你需要装饰dispatch方法csrf_exempt才能工作.它的作用是csrf_exempt在视图函数本身上设置一个属性True,中间件在(最外层)视图函数上检查它.如果只需要修饰一些方法,你仍然需要csrf_exemptdispatch方法上使用,但你可以使用csrf_protect例如put().如果GET,HEAD,OPTIONSTRACEHTTP方法用于不管你装饰与否也不会被选中.

class ChromeLoginView(View):
    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(ChromeLoginView, self).dispatch(request, *args, **kwargs)

    def get(self, request):
        return JsonResponse({'status': request.user.is_authenticated()})

    def post(self, request):
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return JsonResponse({'status': True})
        return JsonResponse({'status': False})
Run Code Online (Sandbox Code Playgroud)


Ant*_*ard 75

正如@knbk所说,这是dispatch()必须装饰的方法.

从Django 1.9开始,您可以method_decorator直接在类上使用:

from django.utils.decorators import method_decorator

@method_decorator(csrf_exempt, name='dispatch')
class ChromeLoginView(View):

    def get(self, request):
        return JsonResponse({'status': request.user.is_authenticated()})

    def post(self, request):
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return JsonResponse({'status': True})
        return JsonResponse({'status': False})
Run Code Online (Sandbox Code Playgroud)

这避免了重写dispatch()方法只是为了装饰它.


小智 5

如果您正在寻找 Mixins 来满足您的需求,那么您可以创建一个 CSRFExemptMixin 并在您的视图中扩展它,无需在每个视图中编写上述语句:

class CSRFExemptMixin(object):
   @method_decorator(csrf_exempt)
   def dispatch(self, *args, **kwargs):
       return super(CSRFExemptMixin, self).dispatch(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

然后在你的视图中像这样扩展它。

class ChromeLoginView(CSRFExemptMixin, View):
Run Code Online (Sandbox Code Playgroud)

您可以根据您的需求在任何视图中扩展它,这就是可重用性!:-)

干杯!