'WSGIRequest'对象没有属性'user'Django admin [英] 'WSGIRequest' object has no attribute 'user' Django admin

查看:858
本文介绍了'WSGIRequest'对象没有属性'user'Django admin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 系统检查没有问题(0沉默)。 
2016年6月21日 - 15:26:14
Django版本1.9.7,使​​用设置'librato_chart_sender_web.settings'
启动开发服务器http://127.0.0.1:8000/
使用CONTROL-C退出服务器。
内部服务器错误:/ admin /
追溯(最近的最后一次调用):
文件/Library/Python/2.7/site-packages/django/core/handlers/base.py ,第149行,get_response
response = self.process_exception_by_middleware(e,request)
文件/Library/Python/2.7/site-packages/django/core/handlers/base.py,第147行,在get_response
response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件/Library/Python/2.7/site-packages/django/contrib/admin/sites.py,第265行,in wrapper
return self.admin_view(view,cacheable)(* args,** kwargs)
文件/Library/Python/2.7/site-packages/django/utils/decorators.py,第149行,在_wrapped_view
response = view_func(request,* args,** kwargs)
文件/Library/Python/2.7/site-packages/django/views/decorators/cache.py,第57行,_wrapped_view_func
response = view_func(request,* args,** kwargs)
文件/Library/Python/2.7/site-packages/django/contrib/admin/site s.py,第233行,内部
如果不是self.has_permission(请求):
文件/Library/Python/2.7/site-packages/django/contrib/admin/sites.py ,第173行,in_permission
return request.user.is_active和request.user.is_staff
AttributeError:'WSGIRequest'对象没有属性'user'
[21 / Jun / 2016 15: 26:18]GET / admin / HTTP / 1.1500 78473

我在django中相当新的。 ..但我遵循本教程: https://docs.djangoproject.com/en/1.9/ref / contrib / admin /



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



我已经google了这个问题,但是我仍然无法以任何方式解决我的情况。你可以帮忙吗?



这里是我的 settings.py


$ b $

$ b由Django生成的django-admin startproject 1.11.dev20160523235928



$ b有关此文件的更多信息,请参阅
https://docs.djangoproject.com/en/dev/topics/settings/

有关设置的完整列表及其值,请参阅
https://docs.djangoproject.com/en/dev/ref/settings/


import os

#在项目中构建路径,如下所示:os.path.join(BASE_DIR,...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__ file__)) )


#快速启动开发设置 - 不适合生产
#请参阅https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

#安全警告:保留生产秘密密钥!
SECRET_KEY ='* 1 @ + = wzrqx ^ 6 $ 9z& @ 2 @ d8r(w $ js + ktw45lv2skez(= kz + rwff_'

#安全警告:不要运行调试开始生产!
DEBUG = True

ALLOWED_HOSTS = []


#应用程序定义

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'


#数据库
#https://docs.djangoproject .com / en / dev / ref / settings /#databases

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


#密码验证
#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',
},
]


#国际化
#https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE ='en-us'

TIME_ZONE ='G MT'

USE_I18N = True

USE_L10N = True

USE_TZ = True


#静态文件(CSS,JavaScript,图像)
#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'),
]

admin .py

 从django.contrib导入admin 
从.models import配置

#在这里注册你的模型。

admin.site.register(配置)


解决方案

要解决此问题,请转到settings.py其中有MIDDLEWARE



将其更改为MIDDLEWARE_CLASSES



https://docs.djangoproject.com/ja/1.9/topics/http/middleware/


When I trying to access the admin page it gives me the following error:

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

Im quite new in django ... but i followed this tutorial: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/

I dont have any custom AdminSites and custom AdminModels.

I already googled about this problem but still i cannot solve it for my case in any way. Can you help ?

here is my 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'),
]

and admin.py:

from django.contrib import admin
from .models import Configuration

# Register your models here.

admin.site.register(Configuration)

解决方案

To resolve this go to settings.py where there MIDDLEWARE

Change that to MIDDLEWARE_CLASSES

https://docs.djangoproject.com/ja/1.9/topics/http/middleware/

这篇关于'WSGIRequest'对象没有属性'user'Django admin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆