12👍
According to the FineManual:
During the response phases (process_response() and process_exception() middleware), the classes are applied in reverse order, from the bottom up
So I’d say you’d better add your middleware before the auth and session middlewares (assuming it only processes the response).
This being said, I’m a bit puzzled by the fact that you only have the error on some pages ???
25👍
Ran into the same issue recently, and found that it happened when a url is being accessed without the trailing slash, and the APPEND_SLASH setting is set to true:
Django processes initial request
- CommonMiddleware.process_request
- Redirects to newurl, which has the trailing slash
- process_response is still run in custom middleware
- request.user is not present
- HTTP 301
Django then processes the request of url with trailing slash
- process_response is run in custom middleware
- request.user is now present
Anyone knows why some of the main attributes (user and session) are not accessible in process_response after a permanent redirect?
- Paypal monthly subscription plan settings for first day of the month and making monthly recurring payment – django python
- How does django-nose differ from the default Django test-runner
- How to create custom groups in django from group
- Changing password in Django Admin
17👍
So it has to do with APPEND_SLASH
being applied with via a redirect by Django Common Middleware, preventing the process_request()
in AuthenticationMiddleware
(which adds the user
attribute) from being run but your process_response
still being run.
Here’s how Django Process Middleware ACTUALLY Works (from django/core/handlers/base.py
in Django 1.6)
- You request a URL that does not have a trailing slash. So
yourdomain.com/view
. This starts the middleware flow. - Once the request reaches
CommonMiddleware
, the middleware sees that there is not a slash and returns ahttp.HttpResponsePermanentRedirect(newurl)
. This immediately stops any additionalprocess_requests
from being run, including one inAuthenticationMiddleware
that add theuser
attribute torequest
- Because
CommonMiddleware
did not return an exception (includingHttp404
),django
will now take the response from the middleware and run it through EVERYprocess_response()
in EVERY middleware listed inMIDDLEWARE_CLASSES
, no matter if that middleware’sprocess_request()
had a chance to run.
The only real way to fix this is to either move your code into a process_request()
method located after AuthenticationMiddleware
in MIDDLEWARE_CLASSES
or detect via hasattr()
if the request
object has a user
attribute.
9👍
do you have active this middleware?:
'django.contrib.auth.middleware.AuthenticationMiddleware'
And this middleware run before your middleware?
- How does django-nose differ from the default Django test-runner
- How can I schedule a Task to execute at a specific time using celery?
- Installing django 1.5(development version) in virtualenv
4👍
I had a similar issue, some of my pages dont have the user in the request so in my middleware I do a quick check
if not hasattr(request, 'user'):
return response
- How to have a link in label of a form field
- Django template indentation guideline
- Django's get_current_language always returns "en"
0👍
There could be an exception raised within some middleware or any other code that runs before the django’s AuthenticationMiddleware (which is responsible for assigning the .user to request object).
Then there will be an AttributeError upon accessing the .user variable.
For example, any exception triggered before the AuthenticationMiddleware had a chance to run might cause the error view to execute. You’ll get the error as mentioned in the title of the question, if the error view depends on request.user.
- Django database synchronization for an offline usage
- Accessing django project in LAN systems
- Create a canonical "parent" product in Django Oscar programmatically