getting access to the exception in Django 500.html?
How can I get access to the exception details in 500.html开发者_Go百科?
Easiest way is to write a middleware that overrides process_exception.
http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception
class ProcessExceptionMiddleware(object):
def process_exception(self, request, exception):
response = direct_to_template(request, "my_500_template", {'exception': exception})
response.status_code = 500
return response
You can subclass django.core.handlers.base.BaseHandler
, or better one of the implementations like django.core.handlers.wsgi.WSGIHandler
, and change the handle_uncaught_exception(self, request, resolver, exc_info)
method. The last argument is the exception info as returned by sys.exc_info
. In the case of WSGI, you would define the custom handler in your WSGI file, for instance.
Simply overwriting handler500
in your URLconf won't work because that function does not receive any information about the actual exception.
精彩评论