django 属性错误:“WSGIRequest”对象没有属性“user”

pu3pd22g  于 2023-08-08  发布在  Go
关注(0)|答案(2)|浏览(162)

我正在运行我的Django代码,但是得到这个错误

AttributeError: 'WSGIRequest' object has no attribute 'user'

字符串
我的Django版本是1.8.2,下面是我的中间件类setting.py

MIDDLEWARE_CLASSES = (

'khxia.middlewares.PeeweeConnectionMiddleware',

'django.contrib.sessions.middleware.SessionMiddleware',
'subdomains.middleware.SubdomainURLRoutingMiddleware',
'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',

'referral.middlewares.ReferralRecordMiddleware',
'common.middlewares.ExceptionMiddleware',
)


这是我的完整追溯

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/motion/kuaixiao_env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
    response = self.get_response(request)
  File "/Users/motion/kuaixiao_env/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/motion/kuaixiao_env/lib/python2.7/site-packages/django/core/handlers/base.py", line 268, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/Users/motion/Documents/GitHub/khx2/khxia/home/views.py", line 68, in server_error
    res = render(request, 'home/500.html')
  File "/Users/motion/Documents/GitHub/khx2/khxia/common/shortcuts.py", line 40, in render
    if request.user.is_authenticated():
AttributeError: 'WSGIRequest' object has no attribute 'user'


views.py的第68行是:

def server_error(request):
    res = render(request, 'home/500.html')
    res.status_code = 500
    return res


渲染函数如下所示:

def render(request, template_name, dictionary=None):
    """

    :param request: Django request object
    :param template_name: template name
    :param dictionary: parameter of dictionary
    """
    dictionary = dictionary or {}
    dictionary.update({
        'settings': settings,
        'DEBUG': settings.DEBUG,
        'server_time': int(time.time()),
    })

    if request.user.is_authenticated():
        dictionary['user_hashid'] = hashid.encode(request.user.id)

    temp = template_name.replace('.html', '').split('/')
    if len(temp) == 1:
        module = 'v{name}'.format(name=temp[0].capitalize())
    else:
        name = ''.join([t.capitalize() for t in temp])
        module = 'v{name}'.format(name=name)
    dictionary['load_module'] = module
    return django_render(
        request,
        template_name,
        dictionary
    )

n53p2ov0

n53p2ov01#

这是因为您放置中间件的顺序。
request.user由AuthenticationMiddleware添加到WSGI Request对象中。
地点

'khxia.middlewares.PeeweeConnectionMiddleware',

字符串
这个中间件是AuthenticationMiddleware之后,或者作为最后一个中间件

63lcw9qa

63lcw9qa2#

如果您使用IntelliJ并使缓存无效或删除.iml文件,请在项目文件夹中选择right-click并选择Open Module Settings。在打开的窗口中,选择Project Settings下的Facets,单击+并选择Django。更新Django project rootSettings位置;如果操作正确,manage.pymigrations将被自动定位。请务必同时检查Project设置是否正确Python SDK,以及Modules->Dependencies是否匹配Module SDK

相关问题