Django 500消息在自定义模板中 [英] Django 500 message in custom template

查看:108
本文介绍了Django 500消息在自定义模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个500.html的模板,每当我的应用程序爆炸时都会被加载,但是我想知道是否有任何方式可以在模板中输出异常的消息。



所以如果我这样做:

  raise Exception(你破了!)

当DEBUG标志设置为True但是如何在模板中访问异常消息时,会加载500.html?如下:

  {{exception.message}} 

非常感谢。



G

解决方案看看这个答案:



如何在Django 500.html页面中添加堆栈跟踪?



将异常传递给您的模板/用户是不好的,因为它可能会显示一些内部运行,您不希望外部使用,但如果您真的需要,您可以编写自己的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'

模板

  {{exception_value}} 

更多关于这里: https://docs.djangoproject.com/en/1.6/topics/http/views/#the-500-server-error-view


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

这篇关于Django 500消息在自定义模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆