Django自定义500错误模板不显示request.user [英] Django custom 500 error template not displaying request.user
问题描述
我正在实现自定义404和500模板,但是当404.html模板似乎返回request.user.is_authenticated时,500.html模板无法返回任何内容。我还检查了request.user,它在500页上只是空白。
I'm implementing custom 404 and 500 templates, but while the 404.html template seems to return request.user.is_authenticated fine, the 500.html template fails to return anything. I also checked for request.user and it's just blank on the 500 page.
这是非常奇怪的,因为当我触发500错误,我收到预期的错误报告电子邮件,显然USER已在请求细分中正确定义。以下是我在views.py中使用的代码:
This is very strange, because when I trigger the 500 error, I receive the expected error report e-mail, and it clearly has USER properly defined in the request breakdown. Here's the code I'm using in views.py:
def handler404(request):
response = render_to_response('404.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
def handler500(request):
response = render_to_response('500.html', {},
context_instance=RequestContext(request))
response.status_code = 500
return response
我想知道后台的某些东西(可能在RequestContext中)是否处理了与404不同的500种?我应该提到我也使用django-guardian,虽然我不认为这会影响任何事情。任何想法?
I'm wondering if something in the background (maybe in RequestContext) is treating the 500 different from the 404? I should mention that I'm also using django-guardian, though I don't think this would affect anything in the case. Any ideas?
编辑: 此评论声称500模板不会呈现request.user,因为它报告了一个500服务器错误,所以服务器是不能服事任何东西。有人知道这个吗?似乎应该有一个,因为,像我说的,我收到的错误报告电子邮件中的日志清楚地具有用户名的请求对象。
This comment claims "the 500 template won't render request.user because it is reporting a 500 server error, so the server is not able to serve up anything." Does anyone know a way around this? Seems like there should be one because, like I said, the log I receive in the error report e-mail clearly has the request object with the user name.
编辑2:现在我想知道它是否与django-allauth有关 - 我也使用它。
Edit 2: Now I'm wondering if it has to do with django-allauth -- I'm using it as well.
推荐答案
我想出来了!我不得不将以下行添加到urls.py中:
I figured it out! I had to add the following line to urls.py:
handler500 = "mysite.views.handler500"
非常奇怪404如果没有相当的行,404工作正常,但500行为真的很奇怪。
Very strange how the 404 worked fine without an equivalent line, but 500 acts really weird.
这篇关于Django自定义500错误模板不显示request.user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!