Django 500 message in custom template

后端 未结 2 1421
长发绾君心
长发绾君心 2021-02-20 10:47

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

2条回答
  •  终归单人心
    2021-02-20 11:45

    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

提交回复
热议问题