我正在练django
在我遇到
'WSGIRequest'对象没有属性'user'
,我重新启动项目,它就消失了
今天,我再次遇到了此错误消息。我想解决。
我打开时没关系
本地主机:8000
但是当我尝试打开时
本地主持人:8000 / admin /
发生错误。
我在google和stackoverflow上搜索了此错误消息
我在settings.py中重新排序了我的MIDDLEWARE。它不起作用。
请帮帮我。
错误消息
AttributeError at /admin/
'WSGIRequest' object has no attribute 'user'
Request Method: GET
Request URL: http://localhost:8000/admin/
Django Version: 1.9.8
Exception Type: AttributeError
Exception Value:
'WSGIRequest' object has no attribute 'user'
Exception Location: c:\todocal\myvenv\lib\site-packages\django\contrib\admin\sites.py in has_permission, line 173
Python Executable: c:\todocal\myvenv\Scripts\python.exe
Python Version: 3.4.3
Python Path:
['c:\\todocal',
'C:\\Python34\\Lib',
'C:\\WINDOWS\\SYSTEM32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34',
'c:\\todocal\\myvenv',
'c:\\todocal\\myvenv\\lib\\site-packages']
Server time: Sat, 13 Aug 2016 15:31:02 +0900
它追溯了
Environment:
Request Method: GET
Request URL: http://localhost:8000/admin/
Django Version: 1.9.8
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'todocal_web']
Installed Middleware:
['django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware']
Traceback:
File "c:\todocal\myvenv\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "c:\todocal\myvenv\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "c:\todocal\myvenv\lib\site-packages\django\contrib\admin\sites.py" in wrapper
265. return self.admin_view(view, cacheable)(*args, **kwargs)
File "c:\todocal\myvenv\lib\site-packages\django\utils\decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "c:\todocal\myvenv\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "c:\todocal\myvenv\lib\site-packages\django\contrib\admin\sites.py" in inner
233. if not self.has_permission(request):
File "c:\todocal\myvenv\lib\site-packages\django\contrib\admin\sites.py" in has_permission
173. return request.user.is_active and request.user.is_staff
Exception Type: AttributeError at /admin/
Exception Value: 'WSGIRequest' object has no attribute 'user'
这是settings.py
"""
Django settings for todocal project.
Generated by 'django-admin startproject' using Django 1.10.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
# 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.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'uij5f=ewy^jv$-=lzt!p%+lq2qv179$a6_lr=$774)@+$=a5(y'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'todocal_web',
]
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',
]
ROOT_URLCONF = 'todocal.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 = 'todocal.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/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.10/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.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Seoul'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
答案 0 :(得分:4)
AuthenticationMiddleware
添加了 request.user
。正如您可以从错误消息中轻松看到的那样,您没有加载它。
如@Sergey Gornostaev所述,您的设置有误。如果要提供中间件类列表,请使用MIDDLEWARE_CLASSES
而不是MIDDLEWARE
。
答案 1 :(得分:3)
将settings.py MIDDLEWARE
更改为MIDDLEWARE_CLASSES
。