Django 'CSRFCheck' object has no attribute 'process_request' - python

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

Add try/except statement to enforce_csrf()
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).
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.
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
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)

Related

Is there a way to exclude parameters being autocompleted in IntelliSense?

Is there a way to exclude parameters being autocompleted in IntelliSense?
I have used IntelliJ before, and I recently switched over to VS Code. Upon switching and installing IntelliSense, I have had issues with some of the autocompletes. For example, on tab completion of the print function, I would just expect it to complete as:
print()
but it instead completes as:
print(sep=..., end=..., file=..., flush=...)
Only for some functions does the autocomplete include the parameter suggestions, and not for other, and I cant seem to figure out why. I tried looking through the settings but without much luck. Any help?

Sphinx autoclass works locally, not readthedocs, but other classes and methods work on readthedocs?

I know there's several similar questions, but none of them seem to fit what is happening with me. When I build on readthedocs, it is successful. However, it doesn't show in the documentation, but will show locally. What my issue is, is that it doesn't show certain methods on readthedocs (even though there's a successful build), but it shows locally. In addition, there are certain instances where it shows neither the class and method I want it to show.
I'm not understanding what is going on and how to go about fixing it. I've made commits trying to fix it and I don't want to continue making unnecessary commits.
Links:
Docs example 1 (class shows, method doesn't)
Docs example 2 (class and method don't show, but it's setup the same as above?)
GitHub Project
Local build screenshot (what I should be seeing with the first example link):
Check your build log on RTD for "warning" or "error".
Although I suggested that the issue could be a typo of "pint" versus "point", it turned out to be a case of needing to add point to requirements.txt.

Django Template loader doesn't have "get_contents()" function implemented. Is there a substitute?

I am working on upgrading some older Django code however I am running into issues with the loader function "get_contents()" as it has apparently been deprecated. I'm not sure how to proceed. Should I create a new child claqss from that loader and implement the function myself or is there another way?
I have basically tried looking this problem up on google and the few answers I've come across have been somewhat vague.
I can't show much code, as this is a private project, but I can show one line with the names changed.
templateVariable = loader.get_template('appName/filename.txt')
This is the error I'm getting when I try and run the function:
'Loader' object has no attribute 'get_contents'
So it turns out that there was a custom loader being used in the project and the get_contents() function hadn't been implemented yet. Another mystery solved.

Import issues with Python and celery

There is a pretty strange issue happening on my production server that doesn't come up in UAT or my development environment. Stack setup is Ubuntu 14LTS, Apache2, Python 2.7,Celery, RabbitMQ, Django 1.6.
How it works is that there is an async task that is called whenever an excel report needs to be generated. Depending on the type of company, a different report is created and emailed to the logged in user. The functions to create the different reports are all placed in a file called report_lib.py and they are not importing from any other app. The code goes like this:
if policies.carrier == "class_one":
report_lib.create_remittance_class_one_report(company_id)
else:
report_lib.create_generic_remittance_report(company_id)
Get this, the first conditional works no problem. The second conditional works when I don't have a company type set(works as expected), but for any other class it fails with the following error:
[2014-10-27 17:56:55,271: WARNING/Worker-1] There was an error: 'module' object has no attribute 'create_generic_remittance_report'
To make things more interesting I removed the conditional and defaulted to the code just calling the "create_generic_remittance_report" function for all cases, which works without complaining about that module.
I'm 99% sure that there are no-circular references. Oh, and the library being referenced is under the only app being used in the django project. Could this be a compiler caching issue?
Has anyone come across a similar issue using the same setup?
Please help!
Found the issue, the function being referenced was indented an extra tab. I guess since celery only calls it, it came back with that error.

Correct place to put extra startup code in django?

I would like to run some environment checks when my django process starts and die noisily in the case of an error. I'm thinking things like the database has an incorrect encoding or the machine has a python version we don't support.
I'd rather our team be faced with a fatal error that they have to fix, rather than be able to ignore it.
I'm Ok with writing these checks but I'm curious about where the best place to put them is. How do I get them to execute as part of django's startup process? I thought there might be a signal I could listen too, but I can't find a relevant one in the docs.
If you don't want to use settings module, then try project's __init__.py.
If you want to check that the system is correctly installed, I think that you should write your own admin command and run it as post-installation check.
I think that it doesn't worth to check if the python version is correctly installed too often especially if you are installing the django app on shared-host. My app is hosted at alwaysdata and they restart the FastCgi process every hour. These checks can have an impact on the application response time.
We use the top-level urls.py for this.
I would put them in settings.py. In the past, I have put system checks like this:
try:
from local_settings import *
except ImportError:
print "Missing %s" % os.path.join(PROJECT_ROOT, "local_settings.py")
if DEBUG:
for p in [PROJECT_ROOT, MEDIA_ROOT, THEME_DIR, ADMIN_MEDIA_ROOT] + list(TEMPLATE_DIRS):
p = os.path.normpath(p)
if not os.path.exists(p):
print "Missing path: %s" % p
I have tested all three __init__.py Settings.py and urls.py methods and this is what I have found.
When code is run from either __init__.py or Settings.py, the start up functions are run twice upon web server start up; when the start up functions are run from urls.py the code is run once, however it is run only upon the first request made to the we server leading to a potentially long wait for the first user to visit your site.
It is standard practise to call a 'warming' page when bringing large web applications back online so I don't see that calling a start up function from a CLEARLY identified location in the urls.py should be a problem.
Most of the answers here are extremely old, so I assume that's why Django's checks framework isn't mentioned.
It's been a part of django since at least v2 (django==3.1 at the time of writing).
That's probably the right place for most of the checks you're requiring.
I'd still consider using settings, as mentioned by other answers, for checks where settings contents are required but which also need to be run prior to readying the apps.
You can put it in settings.py as mentioned by others, but having code in the settings is not ideal. There is also the option of adding a handler for django.db.models.signals.class_prepared that does the desired start up checks after a specific model class is prepared.

Categories