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

Gon*_*eia 62 python django wsgi admin panel

当我尝试访问管理页面时,它给出了以下错误:

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
Run Code Online (Sandbox Code Playgroud)

我是django的新手...但我遵循了这个教程:https://docs.djangoproject.com/en/1.9/ref/contrib/admin/

我没有任何自定义AdminSites和自定义AdminModels.

我已经用谷歌搜索了这个问题,但我仍然无法以任何方式解决这个问题.你能帮我吗 ?

这是我的settings.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'),
]
Run Code Online (Sandbox Code Playgroud)

并且admin.py:

from django.contrib import admin
from .models import Configuration

# Register your models here.

admin.site.register(Configuration)
Run Code Online (Sandbox Code Playgroud)

小智 84

要解决这个问题,请转到settings.py新风格的地方MIDDLEWARE(在Django 1.10中引入)

将其改为旧式 MIDDLEWARE_CLASSES

https://docs.djangoproject.com/en/stable/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware

  • @asgaines如果你正在运行django 1.10你不需要这样做,MIDDLEWARE是一个1.10+设置.virtualenv也不是虚拟机. (8认同)
  • 如果使用Django 1.10和virtualenv,请确保在启动`python manage.py runserver之前虚拟机正在运行 (4认同)
  • 不推荐使用此方法.如果未正确排序中间件类的顺序,则会发生此错误.但是,解决此问题的最佳方法是将'django.contrib.sessions.middleware.SessionMiddleware'设置为'django.contrib.auth.middleware.AuthenticationMiddleware'之前的第一个中间件,其他人遵循. (2认同)

Gon*_*eia 67

我找到了答案.变量名称:

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',
]
Run Code Online (Sandbox Code Playgroud)

MIDDLEWARE是Django 1.10中引入的新式配置.将名称更改为MIDDLEWARE_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',
]
Run Code Online (Sandbox Code Playgroud)

  • MIDDLEWARE_CLASSES在1.10中更改为MIDDLEWARE.(https://docs.djangoproject.com/en/1.10/topics/http/middleware/)如果您使用的是以前的版本(<= 1.9),请使用MIDDLEWARE_CLASSES. (7认同)
  • 我很高兴你弄清楚了.另外,您可能希望为项目生成新的密钥,因为您已将当前的密钥发布到公共域中(不是很好!) (6认同)

kay*_*yoz 36

如果有任何人在使用Django 2.0时出现此问题,以下配置采用新风格MIDDLEWARE似乎有效(此处docs):

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',
]
Run Code Online (Sandbox Code Playgroud)

  • 换句话说:删除dj2.0中的SessionAuthenticationMiddleware (4认同)
  • 其他答案似乎听起来像Django已弃用的“ MIDDLEWARE”,并在某个时候更改为“ MIDDLEWARE_CLASSES”,但这令人产生误解。“ MIDDLEWARE_CLASSES”一直是老方法,而“ MIDDLEWARE”一直是新样式。我已经编辑了其他答案以使这个答案以及这个答案更加清晰。 (2认同)

小智 20

如果任何人在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',
)
Run Code Online (Sandbox Code Playgroud)

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',
]
Run Code Online (Sandbox Code Playgroud)

它对我有用,因为我使用django 1.0.x创建了我的项目,但后来更新为django 2.0.2

  • 最后,只有这对我有用:使用 MIDDLEWARE 而没有 'django.contrib.auth.middleware.SessionAuthenticationMiddleware'。 (3认同)
  • 这是我从 Django 1.11 升级到 2.1 的正确解决方案:(1)将“MIDDLEWARE_CLASSES”元组更改为“MIDDLEWARE”列表(2)删除“django.contrib.auth.middleware.SessionAuthenticationMiddleware” (2认同)

Jos*_*lgo 15

您不应该将MIDDLEWARE更改为MIDDLEWARE_CLASSES.这里发生的事情更有可能是您使用django 1.10创建了应用程序,现在您使用1.9或以前的版本运行它.

确保使用特定版本的django(以及所有其他库),以便在不同的计算机上部署或运行​​时,项目不会中断.

如果你有一个稳定的代码库,只需运行:

pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

然后在部署或设置新环境时,请执行以下操作:

pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)

您应该始终考虑使用库的固定版本(并且希望是虚拟环境),并且在升级依赖项时测试每个版本的更改.

  • 我认为这是最好的答案;不要像其他答案所说的那样降级到较旧的功能,除非您必须这样做。 (2认同)