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錯誤。@csrf_exempt不能在基於通用視圖的類上工作
但是,如果去除裝飾和做這個URL配置
url(r'^chrome_login/', csrf_exempt(ChromeLoginView.as_view()), name='chrome_login'),
它會奏效。
這裏發生了什麼事?它不應該工作,因爲我猜這就是method_decorator所做的。 我使用python3.4和django1.7.1
任何意見都會很好。
你應該看看django_braces ... – rnevius 2014-12-05 13:28:57
@rnevius非常感謝,以前從來不知道這個mixin lib。 – castiel 2014-12-05 14:31:20
超級棒!特別是因爲您可以簡單地將[CsrfExemptMixin](https://django-braces.readthedocs.org/en/v1.4.0/form.html#csrfexemptmixin)添加到您的視圖以使其工作。這幾乎就像是作弊... – rnevius 2014-12-05 14:37:29