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

后端 未结 8 1269
感动是毒
感动是毒 2020-11-29 01:08

When I trying to access the admin page it gives me the following error:

System check identified no issues (0 silenced).
June 21, 2016 - 15:26:14
Django versi         


        
相关标签:
8条回答
  • 2020-11-29 01:42

    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',
    ]
    
    0 讨论(0)
  • 2020-11-29 01:45

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

    0 讨论(0)
  • 2020-11-29 01:50

    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.

    0 讨论(0)
  • 2020-11-29 01:51

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

    0 讨论(0)
  • 2020-11-29 01:57

    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',
    ]
    
    0 讨论(0)
  • 2020-11-29 02:02

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

    0 讨论(0)
提交回复
热议问题