I have a 500.html template which gets loaded whenever my app explodes but I wanted to know if there's any way I can output the exception's message in the template?
So if I do this:
raise Exception("You broke it!")
This will load 500.html when the DEBUG flag is set to True but how can I access the exception message in the template? Something like:
{{ exception.message }}
Many thanks.
G
Have a look at this answer:
How do I include a stacktrace in my Django 500.html page?
It's not good to pass the exception to your template/user as it might show some inside workings that you don't want available to the outside, but if you really need to, you could write your own 500 view, grabbing the exception and passing it to your 500 template
views.py
def custom_500(request):
t = loader.get_template('500.html')
type, value, tb = sys.exc_info(),
return HttpResponseServerError(t.render(Context({
'exception_value': value,
})))
somewhere in urls.py
handler500 = 'mysite.views.my_custom_error_view'
template
{{ exception_value }}
more about it here: https://docs.djangoproject.com/en/1.6/topics/http/views/#the-500-server-error-view
I know this is an old thread, but I just wanna to make it clear:
The answer is correct if you want a custon view! The default view will load a 500.html (also 404.html and others), from the main templates directory of your project if it's there.
So, if all you need is to change static page content, like insert some images in your error page, all you have to do is to create a template file at this place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With