你需要装饰工作
dispatch方法
csrf_exempt。它所做的是将
csrf_exempt视图函数本身的属性设置为
True,然后中间件在(最外面的)视图函数中对此进行检查。如果只需要修饰几种方法,则仍然需要
csrf_exempt在该
dispatch方法上使用,但是可以
csrf_protect在例如上使用put()。如果
GET,HEAD,OPTIONS或
TRACE使用HTTP方法不管你把装修与否也不会被选中。
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})


