Django错误:"WSGIRequest"对象没有属性"user" [英] Django Error : 'WSGIRequest' object has no attribute 'user'

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

问题描述


我正在练习django.
在遇到此错误消息之前,


I'm practicing django.
Before I met this error message that is

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

'WSGIRequest' object has no attribute 'user'

,我重新启动项目,它消失了.
今天,我再次遇到此错误消息.我要修复.
我打开

, I restart project and it was disappeared.
Today, I met this error message again. I want to fix.
It's OK when I open

localhost:8000

localhost:8000

但是当我尝试打开

本地主机:8000/admin/

local host:8000/admin/

发生错误.

我在Google和stackoverflow上搜索了此错误消息.
我在settings.py中重新排序了我的MIDDLEWARE.它不起作用.
请帮帮我.

I searched this error message at google and stackoverflow.
I reordered my MIDDLEWARE in settings.py. It doesn't work.
Please help me.

错误消息

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

It's 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')

推荐答案

request.user

request.user is added by AuthenticationMiddleware. As you can easily see from the error message, you don't have that loaded.

如@Sergey Gornostaev所述,您的设置错误.如果要提供中间件类的列表,请使用 MIDDLEWARE_CLASSES 而不是仅 MIDDLEWARE .

As noted by @Sergey Gornostaev, your settings are wrong. If you want to provide a list of middleware classes, use MIDDLEWARE_CLASSES instead of just MIDDLEWARE.

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

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