I want to set a cookie if user is logged in or not.
My Middleware:
class UserStatus(object):
def process_response(self,request,response):
user_status = 1 if request.user.is_authenticated() else 0
max_age = (20)*52*7*24*60*60 # 20 years (After expiry, cookie gets deleted)
response.set_cookie(user_status_cookie,user_status,max_age)
return response
Added to MIDDLEWARE_CLASSES
in settings.py at the end.
Problem:
- Error: 'WSGIRequest' object has no attribute 'user'
- Why, when I have the Authentication and the Session middlewares active already ?
- Also, some pages are working smooth where as some are giving this error.
- What am I doing wrong ?
Please help.
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
Django then processes the request of url with trailing slash
Anyone knows why some of the main attributes (user and session) are not accessible in process_response after a permanent redirect?
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
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.
According to the FineManual:
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 ???
So it has to do with
APPEND_SLASH
being applied with via a redirect by Django Common Middleware, preventing theprocess_request()
inAuthenticationMiddleware
(which adds theuser
attribute) from being run but yourprocess_response
still being run.Here's how Django Process Middleware ACTUALLY Works (from
django/core/handlers/base.py
in Django 1.6)yourdomain.com/view
. This starts the middleware flow.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
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 afterAuthenticationMiddleware
inMIDDLEWARE_CLASSES
or detect viahasattr()
if therequest
object has auser
attribute.do you have active this middleware?:
And this middleware run before your middleware?