我正在运行我的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
)
型
2条答案
按热度按时间n53p2ov01#
这是因为您放置中间件的顺序。
request.user由AuthenticationMiddleware添加到WSGI Request对象中。
地点
字符串
这个中间件是AuthenticationMiddleware之后,或者作为最后一个中间件
63lcw9qa2#
如果您使用IntelliJ并使缓存无效或删除.iml文件,请在项目文件夹中选择
right-click
并选择Open Module Settings
。在打开的窗口中,选择Project Settings
下的Facets
,单击+
并选择Django
。更新Django project root
和Settings
位置;如果操作正确,manage.py
和migrations
将被自动定位。请务必同时检查Project
设置是否正确Python SDK
,以及Modules->Dependencies
是否匹配Module SDK
。