疑难解答'WSGIRequest'对象没有属性'user'? [英] Troubleshooting 'WSGIRequest' object has no attribute 'user'?
问题描述
我试图使用 @login_required
装饰器,但是我得到一个'WSGIRequest'对象没有属性'user'
AttributeError。我从顶部的django.contrib.auth.decorators导入login_required 中的import语句,但我仍然收到错误。当我在我的views.py中删除
def profile(request)
前面的 @login_required
时,问题消失了当我把它放在那里时,问题依然存在。
I'm trying to use the @login_required
decorator but I get a 'WSGIRequest' object has no attribute 'user'
AttributeError. I have the import statement from django.contrib.auth.decorators import login_required
on the top but I still get the error. When I delete @login_required
from in front of def profile(request)
in my views.py the problem goes away but when I put it in there the problem is still there.
来自setting.py
Middleware classes from setting.py
MIDDLEWARE_CLASSES = (
#~ 'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
#~ 'django.middleware.csrf.CsrfViewMiddleware',
#~ 'django.contrib.auth.middleware.AuthenticationMiddleware',
#~ 'django.contrib.messages.middleware.MessageMiddleware',
)
推荐答案
您需要启用'django.contrib.auth.middleware .AuthenticationMiddleware',因为它设置了request.user。您还应该启用其他默认中间件,否则您将遇到其他问题。
You need to enable 'django.contrib.auth.middleware.AuthenticationMiddleware', because it sets request.user. You should also enable the other default middleware or you're going to have other problems.
这篇关于疑难解答'WSGIRequest'对象没有属性'user'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!