gpt4 book ai didi

python - AttributeError: 'WSGIRequest' 对象没有属性 'user'

转载 作者:行者123 更新时间:2023-11-30 22:22:45 25 4
gpt4 key购买 nike

我正在运行 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
)

最佳答案

这是因为您放置中间件的顺序。

request.user 由 AuthenticationMiddleware 添加到 WSGI 请求对象。

地点

'khxia.middlewares.PeeweeConnectionMiddleware',

此中间件位于 AuthenticationMiddleware 之后,或作为最后一个中间件

关于python - AttributeError: 'WSGIRequest' 对象没有属性 'user',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48222837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com