Django:WSGIRequest对象在某些页面上没有属性“user” [英] Django: WSGIRequest' object has no attribute 'user' on some pages?
问题描述
如果用户登录,我想设置一个cookie。
I want to set a cookie if user is logged in or not.
我的中间件
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
在设置中添加到 MIDDLEWARE_CLASSES
问题:
- 错误:'WSGIRequest'对象没有属性'user'
- 为什么我的身份验证和会话中间件已经活动?
- 另外,一些页面工作顺利,有些人正在给出这个错误。
- 我做错了什么?
- 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 ?
请帮忙。
推荐答案
根据FineManual:
According to the FineManual:
在响应阶段(process_response()和process_exception() ),这些类按照相反的顺序应用,从下到上
During the response phases (process_response() and process_exception() middleware), the classes are applied in reverse order, from the bottom up
所以我会说你最好添加你的中间件em> before auth和session middlewares(假设它只处理响应)。
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 ???
这篇关于Django:WSGIRequest对象在某些页面上没有属性“user”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!