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

廉价感情. 提交于 2019-11-27 03:29:23
Sir Mbuki

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

Gonçalo Correia

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',
]
kayoz

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',
]

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

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.

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

Simple.....

If you have picked the code from somewhere else(mean project is not created on your pc)... then their may be a different configuration of MIDDLEWARE in setting file in your code.....so you need to just replace that MIDDLLEWARE from the one that your django produces( create a throwaway project->go to setting files ---> copy that MIDDLEWARE part and paste it in the project in which you are getting error).

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:

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!