问题
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.
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
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?
Edit: 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.
Edit 2: Now I'm wondering if it has to do with django-allauth -- I'm using it as well.
回答1:
I figured it out! I had to add the following line to urls.py:
handler500 = "mysite.views.handler500"
Very strange how the 404 worked fine without an equivalent line, but 500 acts really weird.
来源:https://stackoverflow.com/questions/26043211/django-custom-500-error-template-not-displaying-request-user