Django @csrf_exempt不适用于基于通用视图的类(Django @csrf_exempt does not work on generic view based class)

2023-05-16编程技术118877

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

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

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

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

它会工作。

这里发生了什么?它不应该工作,因为我猜这就是method_decorator所做的。我正在使用python3.4和django1.7.1,任何建议都会很棒。

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

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

正如@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})

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

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

class CSRFExemptMixin(object):
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(CSRFExemptMixin, self).dispatch(*args, **kwargs)

之后在你的视图中扩展这个。

class ChromeLoginView(CSRFExemptMixin, View):

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

干杯!

Django @csrf_exempt不适用于基于通用视图的类(Django @csrf_exempt does not work on generic view based class)的相关教程结束。

本文地址:https://www.ufcn.cn/tutorials/2644477.html

如非特殊说明,本站内容均来自于网友自主分享,概不代表本站观点,如有任何问题我们都将在收到反馈后的第一时间进行处理!