Django管理员错误' WSGIRequest'对象没有属性'用户

时间:2018-01-04 17:03:00

标签: python django

我正在练习Django,当我尝试去http://localhost/admin/时我得到以下错误,我检查了settings.py和MIDDLEWARE_CLASSES确实存在,是否还有其他原因我收到此消息。< / p>

    AttributeError at /admin/
'WSGIRequest' object has no attribute 'user'
Request Method: GET
Request URL:    http://localhost/admin/
Django Version: 2.0
Exception Type: AttributeError
Exception Value:    
'WSGIRequest' object has no attribute 'user'
Exception Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/sites.py in has_permission, line 186
Python Executable:  /usr/local/bin/python3
Python Version: 3.6.1
Python Path:    
['/Users/yasserhussain/learning_site',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']
Server time:    Thu, 4 Jan 2018 00:17:34 +0000

设置中的MIDDLEWARE内容

MIDDLEWARE = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

2 个答案:

答案 0 :(得分:5)

Django 2.0已放弃对MIDDLEWARE_CLASSES changelog, last item

中旧式中间件类的支持

如果仅使用标准中间件,则升级很简单,如Upgrading pre-Django 1.10-style middleware中所述。 通常只需将MIDDLEWARE_CLASSES重命名为MIDDLEWARE并删除'django.contrib.auth.middleware.SessionAuthenticationMiddleware'即可。

您还必须确保django.contrib.auth is installed properly。除了您已有的SessionMiddleware之外,您还应将django.contrib.auth.middleware.AuthenticationMiddleware添加到MIDDLEWARE设置。

答案 1 :(得分:2)

我原来的Django版本是1.9。升级到2.0.5版后,我遇到了同样的问题。 将MIDDLEWARE_CLASSES重命名为MIDDLEWARE并删除&#39; django.contrib.auth.middleware.SessionAuthenticationMiddleware&#39;在settings.py中完成了这项工作