Python Django 1.5.1登录\u所需的装饰程序无法捕获未经身份验证的用户

Python Django 1.5.1登录\u所需的装饰程序无法捕获未经身份验证的用户,python,django,login-required,Python,Django,Login Required,我有一个非常典型的视图/登录所需的decorator实现,据我所知,QA团队有时一天会遇到两次这样的错误: ERROR: AttributeError at /plan/reviewplan/1702/ 'WSGIRequest' object has no attribute 'user' Request Method: GET Request URL: http://<ip>/plan/reviewplan/1702/ Django Version: 1.5.1 Trace

我有一个非常典型的视图/登录所需的decorator实现,据我所知,QA团队有时一天会遇到两次这样的错误:

ERROR: AttributeError at /plan/reviewplan/1702/ 'WSGIRequest' object has no 
attribute 'user' Request Method: GET Request URL: 
http://<ip>/plan/reviewplan/1702/ Django Version: 1.5.1

Traceback: File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response 187. 
response = middleware_method(request, response) 
File "/usr/local/lib/python2.6/dist-packages/debug_toolbar/panels/template.py" in process_response 118. pformat(k(self.request))) for k in get_standard_processors() 
File "/opt/ion/iondb/rundb/context_processors.py" in base_context_processor 25. 
if request.user: Exception Type: AttributeError at /plan/reviewplan/1702/ 
Exception Value: 'WSGIRequest' object has no attribute 'user' 
仅供参考:ctx实例存储在会话中,并且经常在视图调用之间更新。我继承了这个设计,对此我无能为力。处理此问题的函数是:

def _create_context_from_session(request, next_step_name):
    ctxd = request.session['saved_plan']
    ctxd['helper'] = request.session['plan_step_helper']
    ctxd['step'] = None
    if next_step_name in ctxd['helper'].steps:
        ctxd['step'] = ctxd['helper'].steps[next_step_name]
    context = RequestContext(request, ctxd)
    return context

会话中存储的上下文变量可能存储了一个没有用户属性的旧请求实例?可能会修改返回的上下文以包含当前请求,如下所示:

def _create_context_from_session(request, next_step_name):
    ctxd = request.session['saved_plan']
    ctxd['request'] = request  # Add this line.
    ...
您的模板\u上下文\u处理器中是否有django.contrib.auth.context\u processors.auth

您似乎还启用了调试工具栏。禁用调试工具栏时是否存在问题

此外:

调试工具栏正在其中间件中执行模板上下文处理器,您的上下文处理器之一尝试访问request.user

您能否修改此回溯中提到的行:

File "/opt/ion/iondb/rundb/context_processors.py" in base_context_processor 25. 
if request.user: Exception Type: AttributeError at /plan/reviewplan/1702/ 
而不是if request.user:do if hasattrrequest,“user”:在/opt/ion/iondb/rundb/context_processors.py文件中


/opt/ion/iondb/rundb/context\u processors.py上下文处理器是否在django.contrib.auth.context\u processors.auth之后执行?上下文处理器的执行顺序由TEMPLATE\u context\u processors设置定义。

传递给\u create\u context\u form\u会话的请求实例是传递给view函数的请求变量,并且从不存储在会话中。另外,为什么调试工具栏会影响request.user?是的,在settings.py.Edited我的答案中添加了django.contrib.auth.context_processors.auth,这一次可能会更有用。“django.contrib.auth.context_processors.auth”是列表中的第一个,然后是“django.core.context_processors.request”,接下来是定制的。你对中间件类和模板上下文处理器的价值是什么?如果禁用django调试工具栏,是否会遇到类似的问题?
File "/opt/ion/iondb/rundb/context_processors.py" in base_context_processor 25. 
if request.user: Exception Type: AttributeError at /plan/reviewplan/1702/