我有一个 500.html 模板,每当我的应用程序爆炸时就会加载该模板,但我想知道是否有任何方法可以在模板中输出异常消息?
所以如果我这样做:
raise Exception("You broke it!")
当 DEBUG 标志设置为 True 时,这将加载 500.html,但如何访问模板中的异常消息?就像是:
{{ exception.message }}
非常感谢。
G
看看这个答案:
如何在 Django 500.html 页面中包含堆栈跟踪? https://stackoverflow.com/questions/121439/how-do-i-include-a-stacktrace-in-my-django-500-html-page
将异常传递给模板/用户并不好,因为它可能会显示一些您不希望外部使用的内部工作,但如果您确实需要,您可以编写自己的 500 视图,捕获异常并传递将其添加到您的 500 模板中
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,
})))
某处urls.py
handler500 = 'mysite.views.my_custom_error_view'
template
{{ exception_value }}
更多相关信息请点击这里:https://docs.djangoproject.com/en/1.6/topics/http/views/#the-500-server-error-view https://docs.djangoproject.com/en/1.6/topics/http/views/#the-500-server-error-view
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)