Python @csrf_豁免不适用于基于通用视图的类

Python @csrf_豁免不适用于基于通用视图的类,python,django,Python,Django,我希望post确实被csrf停止,但它返回403错误 但如果删除该装饰器并在URLConf中执行此操作 class ChromeLoginView(View): def get(self, request): return JsonResponse({'status': request.user.is_authenticated()}) @method_decorator(csrf_exempt) def post(self, request

我希望post确实被csrf停止,但它返回403错误

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

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})
它会起作用的

这里发生了什么事?难道它不应该起作用吗,因为我想这就是我们的装修师所做的。 我正在使用python3.4和django1.7.1


任何建议都会很好。

您需要装饰
调度
方法,以便
csrf\u豁免
工作。它所做的是将视图函数本身上的
csrf\u employ
属性设置为
True
,中间件在(最外面的)视图函数上检查这一点。如果只有少数方法需要修饰,您仍然需要在
dispatch
方法上使用
csrf\u-emption
,但您可以在例如
put()
上使用
csrf\u-protect
。如果使用了
GET
HEAD
OPTIONS
TRACE
HTTP方法,则不会检查是否装饰它

url(r'^chrome_login/', csrf_exempt(ChromeLoginView.as_view()), name='chrome_login'),

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

自Django 1.9以来:


这避免了仅为了修饰而重写
dispatch()
方法。

如果您正在寻找符合您需要的mixin,那么您可以创建一个CSRFExemptMixin并在视图中扩展它,而无需在每个视图中编写上述语句:

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})
在那之后,在你的视野中像这样扩展它

class CSRFExemptMixin(object):
   @method_decorator(csrf_exempt)
   def dispatch(self, *args, **kwargs):
       return super(CSRFExemptMixin, self).dispatch(*args, **kwargs)
您可以根据需要在任何视图中扩展它,这就是可重用性!:-)

干杯

为此提供了一个
CsrfExemptMixin

class ChromeLoginView(CSRFExemptMixin, View):

你应该看看django_牙套…@rnevius非常感谢,以前从来都不知道django的混音库。它太棒了!特别是因为您可以简单地向视图中添加。这几乎就像作弊……在我的用例中,我没有使用表单,那么我如何在我的应用程序中仍然包含csrf令牌呢?就像我正在创建API,所以UI不会出现在图片中。那么,我如何确保CSRF令牌仍然被传递?这些答案对DRF不起作用。看见
class ChromeLoginView(CSRFExemptMixin, View):
from braces.views import CsrfExemptMixin

class ChromeLoginView(CsrfExemptMixin, View):
    ...