[Django]-'WSGIRequest' object has no attribute 'user' Django admin

91πŸ‘

βœ…

To resolve this go to settings.py where there is new-style MIDDLEWARE (introduced in Django 1.10)

Change that to old-style MIDDLEWARE_CLASSES

https://docs.djangoproject.com/en/stable/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware

πŸ‘€Sir Mbuki

72πŸ‘

I found the answer. The variable name on:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

MIDDLEWARE is the new-style configuration introduced in Django 1.10. Change the name to MIDDLEWARE_CLASSES and now it works.

So now the code is:

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

42πŸ‘

In case anyone is having this problem with Django 2.0, the following configuration with new-style MIDDLEWARE seems to work (docs here):

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
πŸ‘€kayoz

26πŸ‘

In case anyone is having the same problem in django 2.0.2 or later,

just update

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

with

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

It worked for me cause i created my project with
django 1.0.x but later updated to django 2.0.2

16πŸ‘

You should not change MIDDLEWARE to MIDDLEWARE_CLASSES. What happens here is more likely that you created the app with django 1.10 and now you are running it with 1.9 or a previous version.

Make sure you use a specific version of django(and all other libraries) so your project doesn’t break when deploying or running on different machines.

If you have a stable codebase simply run:

pip freeze > requirements.txt

And then when deploying or setting up a new env just do:

pip install -r requirements.txt

You should always consider using fixed versions of your libraries(and hopefully virtual envs), and when upgrading dependencies test each version change.

πŸ‘€Jose Hidalgo

3πŸ‘

I had a similar error in my production server and thanks to sentry’s breadcrumbs I saw that the error that was raising had to do with my settings, especially the ALLOWED_HOSTS.

Django version 1.10.8 with python 2.7.

My previous settings:

ALLOWED_HOSTS = ['0.0.0.0',
                 'beta.mydomain.co.uk',
                 'mydomain.co.uk',
                 'www.mydomain.co.uk',
                 'alpha.mydomain.co.uk']

Sentry Breadcrumbs screen shot:
sentry breadcrumb screenshot

After that I looked around and found this:

A value beginning with a period can be used as a subdomain wildcard:
β€˜.example.com’ will match example.com, www.example.com, and any other
subdomain of example.com.

Link to Django official docs

So my final settings that solved my problem:

ALLOWED_HOSTS = ['0.0.0.0',
                 'mydomain.co.uk',
                 'www.mydomain.co.uk',
                 '.mydomain.co.uk']

Hope this was useful πŸ™‚

πŸ‘€Madox

2πŸ‘

My solution was that my Django ver. was 1.9 I reinstalled back to 1.10 without changing MIDDLEWARE to MIDDLEWARE_CLASSES.

Leave a comment