Python ';WSGIRequest&x27;对象没有属性';用户';Django管理员
当我尝试访问管理页面时,它会显示以下错误:Python ';WSGIRequest&x27;对象没有属性';用户';Django管理员,python,django,admin,panel,wsgi,Python,Django,Admin,Panel,Wsgi,当我尝试访问管理页面时,它会显示以下错误: System check identified no issues (0 silenced). June 21, 2016 - 15:26:14 Django version 1.9.7, using settings 'librato_chart_sender_web.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL
System check identified no issues (0 silenced).
June 21, 2016 - 15:26:14
Django version 1.9.7, using settings 'librato_chart_sender_web.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /admin/
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py", line 265, in wrapper
return self.admin_view(view, cacheable)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py", line 233, in inner
if not self.has_permission(request):
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py", line 173, in has_permission
return request.user.is_active and request.user.is_staff
AttributeError: 'WSGIRequest' object has no attribute 'user'
[21/Jun/2016 15:26:18] "GET /admin/ HTTP/1.1" 500 78473
我是django的新手。。。但我遵循了本教程:
我没有任何自定义管理员站点和自定义管理员模型
我已经在谷歌上搜索过这个问题,但我仍然无法以任何方式解决我的问题。你能帮忙吗
这是我的设置。py
:
"""
Django settings for librato_chart_sender_web project.
Generated by 'django-admin startproject' using Django 1.11.dev20160523235928.
For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/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/dev/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*1@+=wzrqx^6$9z&@2@d8r(w$js+ktw45lv2skez(=kz+rwff_'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'librato_chart_sender',
'fontawesome',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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 = 'librato_chart_sender_web.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'librato_chart_sender/templates')],
'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 = 'librato_chart_sender_web.wsgi.application'
# Database
# https://docs.djangoproject.com/en/dev/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/dev/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/dev/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/dev/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
('css', 'librato_chart_sender/static/css'),
('js', 'librato_chart_sender/static/js'),
('fonts', 'librato_chart_sender/static/fonts'),
]
from django.contrib import admin
from .models import Configuration
# Register your models here.
admin.site.register(Configuration)
和admin.py
:
"""
Django settings for librato_chart_sender_web project.
Generated by 'django-admin startproject' using Django 1.11.dev20160523235928.
For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/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/dev/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*1@+=wzrqx^6$9z&@2@d8r(w$js+ktw45lv2skez(=kz+rwff_'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'librato_chart_sender',
'fontawesome',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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 = 'librato_chart_sender_web.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'librato_chart_sender/templates')],
'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 = 'librato_chart_sender_web.wsgi.application'
# Database
# https://docs.djangoproject.com/en/dev/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/dev/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/dev/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/dev/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
('css', 'librato_chart_sender/static/css'),
('js', 'librato_chart_sender/static/js'),
('fonts', 'librato_chart_sender/static/fonts'),
]
from django.contrib import admin
from .models import Configuration
# Register your models here.
admin.site.register(Configuration)
我找到了答案。上的变量名:
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',
]
中间件
是Django 1.10中引入的新型配置。将名称更改为MIDDLEWARE\u CLASSES
,现在它就可以工作了
现在代码是:
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',
]
要解决此问题,请转到
settings.py
,其中有新型的中间件(在Django 1.10中引入)
将其更改为旧式MIDDLEWARE\u类
您不应该将中间件更改为中间件类。这里发生的情况更可能是您使用django 1.10创建了应用程序,而现在您使用的是1.9或以前的版本
确保使用特定版本的django(以及所有其他库),以便在不同的机器上部署或运行项目时不会中断
如果您有稳定的代码库,只需运行:
pip freeze > requirements.txt
然后,在部署或设置新环境时,只需执行以下操作:
pip install -r requirements.txt
您应该始终考虑使用您的库的固定版本(并希望使用虚拟EnvS),并且在升级依赖项时测试每个版本的更改。
< P>我的解决方案是Django VER。was 1.9 I重新安装回1.10,但没有将中间件更改为中间件类。如果有人在Django 2.0中遇到此问题,则使用新型中间件的以下配置似乎可以工作():
我在我的生产服务器上也有类似的错误,多亏了sentry的面包屑,我发现引起的错误与我的设置有关,特别是允许的_主机
Django版本1.10.8和python 2.7
我以前的设置:
ALLOWED_HOSTS = ['0.0.0.0',
'beta.mydomain.co.uk',
'mydomain.co.uk',
'www.mydomain.co.uk',
'alpha.mydomain.co.uk']
哨兵面包屑屏幕截图:
之后,我环顾四周,发现:
以句点开头的值可用作子域通配符:
“.example.com”将与example.com、www.example.com和任何其他
example.com的子域
链接到
因此,我的最终设置解决了我的问题:
ALLOWED_HOSTS = ['0.0.0.0',
'mydomain.co.uk',
'www.mydomain.co.uk',
'.mydomain.co.uk']
希望这是有用的:)以防任何人在django 2.0.2或更高版本中遇到同样的问题
只是更新
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',
)
与
它对我很有用,因为我用它创建了我的项目
django 1.0.x,但后来更新为django 2.0.2
如果您从其他地方选择了代码(意味着项目未在您的pc上创建)。。。然后,在代码中设置文件时,它们可能是不同的中间件配置……因此,您只需将该中间件替换为django生成的中间件(创建一次性项目->转到设置文件->复制该中间件部分并将其粘贴到出错的项目中) 我很高兴你明白了。另一方面,您可能希望为您的项目生成一个新的密钥,因为您已经在公共域中发布了当前密钥(不太好!)中间件类在1.10中更改为中间件。()如果您使用的是以前的版本(这种情况发生在我身上,因为我不得不从以前的1.10安装降级到1.9。如果使用Django 1.10和virtualenv,请确保虚拟机正在运行,然后再启动python manage.py runserver
这是不正确的。我同时拥有这两个版本,但仍然会出现此错误。即使我完全删除中间件,也会出现此错误。如果使用jango 1.10和virtualenv,在启动python manage.py runserver
@asgaines之前确保虚拟机正在运行如果您运行的是django 1.10,则不需要这样做,中间件是1.10+设置。而且virtualenv不是虚拟机。这似乎与@GonçaloCorreia的答案相同(更旧),但不太详细…此方法已被弃用。当中间件类的顺序未正确排序时,会发生此错误。但是,解决此问题的最佳方法是将“django.contrib.sessions.middleware.SessionMiddleware”设置为“django.contrib.auth.middleware.AuthenticationMi”之前的第一个中间件对于未来的读者,您只需要修复'WSGIRequest'对象没有属性“user”
的两个对象是SessionMiddleware
和AuthenticationMiddleware
。我认为这是最好的答案;不要像其他答案所说的那样降级到较旧的功能,除非您不得不这样做。其他答案听起来好像Django不赞成使用中间件
,并在某个时候改为中间件类
,但这是误导性的。中间件类
一直是老办法,而中间件
一直是新样式。我编辑了其他答案,以使这一点更清楚,这也是一点wer.换言之:删除dj2.0中的SessionAuthenticationMiddleware所有这些更改中间件文本的建议对我来说都不起作用。我有一个新的2.0.4安装,仅在调试模式下本地运行。它在一周内运行良好,然后今天决定拒绝我访问POST变量。再多玩中间件也不合适混合。@mirek为什么我要删除SessionAuthenticationMiddleware
?@Timo,没有。建议的解决方案对我不起作用。我必须从头开始创建一个全新的Django项目并复制我的文件。最后,只有这一点对我有效:使用中间件,而不是“Django.contrib.auth.MIDDLEWARE.SessionAuthenticationMiddleware”。这对于我来说,从Django 1.11升级到2.1是否是合适的解决方案:(1)将“MIDDLEWARE_CLASSES”元组更改为“MIDDLEWARE”列表(2)删除“Django.contrib.auth.MIDDLEWARE.SessionAuthenticationMiddleware”