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
任何建议都会很棒。
正如@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()
方法只是装饰它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)