Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 500 message in custom template

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

like image 437
givp Avatar asked May 31 '11 15:05

givp


2 Answers

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

like image 92
Timmy O'Mahony Avatar answered Sep 25 '22 00:09

Timmy O'Mahony


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.

like image 21
Gustavo Vargas Avatar answered Sep 23 '22 00:09

Gustavo Vargas