python - Django 1.7 属性错误 : 'WSGIRequest' object has no attribute 'user'

标签 python django apache2

我读过一些类似的帖子,但没有一个对我有帮助,而且建议我创建一个新问题。

我尝试访问继承网站的管理页面,并为此创建了一个用户,但我访问管理页面时得到了以下堆栈跟踪。我还添加了 settings.py,以防有帮助。

非常感谢您的任何意见。

中号

settings.py

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 = 'XXXXX'

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

# ALLOWED_HOSTS = ['0.0.0.0', 'localhost']
ALLOWED_HOSTS = ['192.168.1.36',  'localhost']


# Application definition

INSTALLED_APPS = [

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 'south',
    'CordeliaHanelBackend',
    'tastypie',
    'StudioHanel',
]

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 = 'CordeliaHanelBackend.urls'

PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    '/var/www/CordeliaHanelBackend-master/StudioHanel/templates2',
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # 'DIRS': ['/var/www/CordeliaHanelBackend-master/StudioHanel/templates2'],
        '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 = 'CordeliaHanelBackend.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 = 'UTC'

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/'

STATICFILES_DIRS = (
    # os.path.join(BASE_DIR, "static"),
    # '/var/www/CordeliaHanelBackend-master/StudioHanel/templates2',
    '/var/www/CordeliaHanelFrontend-master/www/dist/',
        # '/var/www/CordeliaHanelFrontend-master/www/app',
        # '/var/www/CordeliaHanelBackend-master/StudioHanel/static',
    )

# STATIC_ROOT = '/var/www/CordeliaHanelFrontend-master/www/app'


STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

# DEFAULT_FILE_STORAGE = '/var/www/CordeliaHanelFrontend-master/www/app'


SOUTH_DATABASE_ADAPTER = {
'default': 'south.db.sqlite3'
}

堆栈跟踪/错误日志

[Wed Nov 16 20:50:07.826886 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] Internal Server Error: /admin/
[Wed Nov 16 20:50:07.826923 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] Traceback (most recent call last):
[Wed Nov 16 20:50:07.826927 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]   File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
[Wed Nov 16 20:50:07.826930 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]     response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Wed Nov 16 20:50:07.826932 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]   File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 225, in wrapper
[Wed Nov 16 20:50:07.826934 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]     return self.admin_view(view, cacheable)(*args, **kwargs)
[Wed Nov 16 20:50:07.826937 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]   File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/utils/decorators.py", line 105, in _wrapped_view
[Wed Nov 16 20:50:07.826939 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]     response = view_func(request, *args, **kwargs)
[Wed Nov 16 20:50:07.826941 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]   File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
[Wed Nov 16 20:50:07.826944 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]     response = view_func(request, *args, **kwargs)
[Wed Nov 16 20:50:07.826946 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]   File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 193, in inner
[Wed Nov 16 20:50:07.826948 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]     if not self.has_permission(request):
[Wed Nov 16 20:50:07.826951 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]   File "/var/www/CordeliaHanelBackend-master/env/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 150, in has_permission
[Wed Nov 16 20:50:07.826953 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644]     return request.user.is_active and request.user.is_staff
[Wed Nov 16 20:50:07.826955 2016] [wsgi:error] [pid 30866:tid 139796884670208] [remote 127.0.0.1:59644] AttributeError: 'WSGIRequest' object has no attribute 'user'

WSGI 文件

import os

master/env/lib/python2.7/site-packages/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CordeliaHanelBackend.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

最佳答案

我遇到了同样的问题,我通过以下方式解决了:

1) 将 MIDDLEWARE 更改为 MIDDLEWARE_CLASSES 2)注释掉SecurityMiddleware

如下所示:

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',
]

关于python - Django 1.7 属性错误 : 'WSGIRequest' object has no attribute 'user' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40642074/

相关文章:

python - 'scrapy' 的网页抓取抓取了 0 个页面和项目

python - 在 Python 中解析和格式化来自 GitHub API 的日期

Django 的 MutiTable 对比。抽象继承

python -/invoice/Flowable <PmlTable@0x1D09C899130 7 rows x 5 cols(tallest row 841)> with cell(0,0) 的布局错误

svn - 使用 Subversion 和 Apache

apache - 在 apache 虚拟主机中重定向和隐藏端口(文件夹) - xwiki

python 文本小部件获取方法

python - 如何将数据框列对转换为单独的字典映射?

python - 对象词在 Django 中自动分配给模型

从命令行执行时 CGI C 输出正确,但从 web 请求执行时不正确