Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin error 'WSGIRequest' object has no attribute 'user

Tags:

python

django

I am practicing Django, and when i try to go to http://localhost/admin/ i get the below error, I have checked settings.py and MIDDLEWARE_CLASSES does exist, is there any other reason why i am getting this message.

    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

content of MIDDLEWARE in settings

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',
)
like image 468
Yasser Hussain Avatar asked Sep 14 '25 09:09

Yasser Hussain


2 Answers

Django 2.0 has dropped support for old-style middleware classes in MIDDLEWARE_CLASSES changelog, last item

Upgrading is straightforward if you use only standard middleware, explained in Upgrading pre-Django 1.10-style middleware. Often just renaming MIDDLEWARE_CLASSES to MIDDLEWARE and removing 'django.contrib.auth.middleware.SessionAuthenticationMiddleware' is enough.

You must also make sure django.contrib.auth is installed properly. In addition to SessionMiddleware, which you already have, you should add django.contrib.auth.middleware.AuthenticationMiddleware to the MIDDLEWARE setting.

like image 82
Jieter Avatar answered Sep 16 '25 00:09

Jieter


My original Django version was 1.9 . I ran into the same issue after upgrading to version 2.0.5. Renaming MIDDLEWARE_CLASSES to MIDDLEWARE and removing 'django.contrib.auth.middleware.SessionAuthenticationMiddleware' in settings.py did the job

like image 20
Ohia George Avatar answered Sep 16 '25 00:09

Ohia George