Yugal Jindle Yugal Jindle - 1 year ago 171
Python Question

Django: WSGIRequest' object has no attribute 'user' on some pages?

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)
return response

Added to
in settings.py at the end.


  • 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.

Answer Source

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 ???

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download