【发布时间】:2015-02-03 14:32:07
【问题描述】:
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
任何建议都会很棒。
【问题讨论】:
-
你应该看看 django_braces...
-
@rnevius 非常感谢,以前从不知道这个用于 django 的 mixin 库。
-
太棒了!特别是因为您可以简单地将CsrfExemptMixin 添加到您的视图中以使其工作。这几乎就像作弊......
-
在我的用例中,我没有使用表单,那么如何在我的应用程序中仍然包含 csrf 令牌?就像我正在创建 API,因此 UI 不会出现。那么如何确保 CSRF 令牌仍然通过?
-
这些答案不适用于 DRF。见stackoverflow.com/questions/30871033/…