/ admin /由Django'WSGIRequest'对象的AttributeError没有属性'user'

时间:2018-03-16 18:46:32

标签: django request admin wsgi attributeerror

我无法打开管理网站,出现错误:

'WSGIRequest'对象没有属性'user'

尝试进入网站http://127.0.0.1:8000/admin/我收到此错误:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/

Django Version: 2.0.3
Python Version: 3.6.4
Installed Applications:
['polls',
 'books',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
[]



Traceback:

File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\joshu\Anaconda3\lib\site-packages\django\contrib\admin\sites.py" in wrapper
  241.                 return self.admin_view(view, cacheable)(*args, **kwargs)

File "C:\Users\joshu\Anaconda3\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "C:\Users\joshu\Anaconda3\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "C:\Users\joshu\Anaconda3\lib\site-packages\django\contrib\admin\sites.py" in inner
  212.             if not self.has_permission(request):

File "C:\Users\joshu\Anaconda3\lib\site-packages\django\contrib\admin\sites.py" in has_permission
  186.         return request.user.is_active and request.user.is_staff

Exception Type: AttributeError at /admin/
Exception Value: 'WSGIRequest' object has no attribute 'user'

错误:'WSGIRequest'对象没有属性'user'

在我的settings.py中,我有这个:

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.9.1.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os
import posixpath

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2314b585-bae2-4083-aa1f-ec9cc78aedb9'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'polls',
    'books',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE_CLASSES = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.security.SecurityMiddleware',
    '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',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'GMT'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static']))

我已经使用mysql3创建了db并且也进行了迁移。

感谢您的帮助!

编辑:

在我更改MIDDLEWARE_CLASSES后,MIDDLEWARE运行命令python manage.py runserver后出现此错误:

C:\Users\joshu\source\repos\mysite\mysite>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2018 - 14:36:53
Django version 2.0.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001BC74AC36A8>
Traceback (most recent call last):
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\utils\module_loading.py", line 20, in import_string
    return getattr(module, class_name)
AttributeError: module 'django.contrib.auth.middleware' has no attribute 'SessionAuthenticationMiddleware'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\servers\basehttp.py", line 44, in get_internal_wsgi_application
    return import_string(app_path)
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\joshu\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\joshu\source\repos\mysite\mysite\mysite\wsgi.py", line 24, in <module>
    application = get_wsgi_application()
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\handlers\wsgi.py", line 140, in __init__
    self.load_middleware()
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 37, in load_middleware
    middleware = import_string(middleware_path)
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\utils\module_loading.py", line 24, in import_string
    ) from err
ImportError: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 140, in inner_run
    handler = self.get_handler(*args, **options)
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 65, in get_handler
    return get_internal_wsgi_application()
  File "C:\Users\joshu\Anaconda3\lib\site-packages\django\core\servers\basehttp.py", line 49, in get_internal_wsgi_application
    ) from err
django.core.exceptions.ImproperlyConfigured: WSGI application 'mysite.wsgi.application' could not be loaded; Error importing module.

更新

我必须在MIDDLEWARE中注释这一行,在settings.py:

'django.contrib.auth.middleware.SessionAuthenticationMiddleware'

现在一切正常! THX!

1 个答案:

答案 0 :(得分:2)

您的设置文件是使用django 1.9创建的。你是runnig django 2.0

Django 2.0改变了中间件设置。

要激活中间件组件,请将其添加到Django设置中的MIDDLEWARE列表中。

更改

MIDDLEWARE_CLASSES = [...]

MIDDLEWARE = [...]