Django'CSRFCheck'对象没有属性'process_request' [英] Django 'CSRFCheck' object has no attribute 'process_request'

查看:64
本文介绍了Django'CSRFCheck'对象没有属性'process_request'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切正常,直到突然在我的API上实现 DjangoObjectPermissions 时出现上述错误。

It was all ok till suddenly I got the above error while implementing DjangoObjectPermissions on my APIs.

之前即使我的生产环境正常,它也可以正常工作。我只在本地环境中看到此错误。

Before this It was working ok even my production environment it is working fine. I am seeing this error on my local environment only.

根据答案,错误会消失,但是我需要知道为什么?

according to this answer, the error would go away, but I need to know why?

请让我知道我应该在此帖子中添加哪些信息。

Please let me know what information should I add to this post.

以下是已安装的相关软件包。

Following are the related packages installed.

Django==1.10
django-allauth==0.29.0
django-angular==0.8.3
django-debug-toolbar==1.6
django-debug-toolbar-request-history==0.0.3
django-debug-toolbar-template-profiler==1.0.1
django-debug-toolbar-template-timings==0.7
djangorestframework==3.5.3


推荐答案

在try_csrf()中添加try / except语句

请注意,此错误源自rest_framework /authentication.py(在SessionAuthentication类中),方法execute_csrf()。 execute_csrf()方法将初始化变量,check = CSRFCheck(),下一行显示 check.process_request(request)。

Note that this error originates from rest_framework/authentication.py, within class SessionAuthentication, method enforce_csrf(). The enforce_csrf() method iinitiates variable, check = CSRFCheck(), and the next line says "check.process_request(request).

如果使用IDE,您将很快注意到CSRFCheck()没有此属性/方法,因此出现了错误。许多开发人员会迅速告诉您升级到django> = 1.11.6,但是您肯定会遇到相同的错误。问题,我正在使用django 1.11.6和rest_framework 3.9.1。

If using an IDE, you'll quickly notice that CSRFCheck() doesn't have this attribute/method thus the error. Many developers will quickly tell you to upgrade to django >= 1.11.6 but you are bound to run into the same error. For that matter, am using django 1.11.6 and rest_framework 3.9.1.

因此,请尝试使用此补救方法,它对我有用。请使用try / except语句。在下面的rest_framework / authentication.py中,检查= CSRFCheck(),
添加此...

So try this remedy, it worked for me. Use the try/except statement. Go within the rest_framework/authentication.py, below, check = CSRFCheck(), add this...

def enforce_csrf(self, request):
   ...
   try:
       check.process_request(request)
   except:
       pass

这是什么意思:
声明 check变量后,调用(尝试)此方法 process_request() ,如果它不起作用(除外),则通过。您将实现两件事这样做:一,您实际上保持了rest_framework源代码的兼容性(假定升级到django的其他版本可能可以解决此问题),二,您可以获得有效的代码(很好!,尤其是在工作敏捷的情况下)

What the lines say is this: After declaring the "check" variable, call (try) this method "process_request()" and if it does not work (except), just pass. You achieve two things with this: One, you literally leave the rest_framework source code compatible (given that upgrading to further versions of django "might" solve this), and two, you get working code (nice!!, especially if working Agile)

这篇关于Django'CSRFCheck'对象没有属性'process_request'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆