在我的某个地方,我抛出了一个错误,尤其是这个错误:
views.py
from xmlrpclib import Fault def some_function(request): if ....: return Fault(-1, 'foo')
然后,也在views.py中,我有自定义500处理程序来捕获服务器错误:
def my_custom_500(request): context = {...} ### Here is where I need to catch `'foo'` ### in order to put it in the context and pass it to the template render(request, '500.html', context)
无论如何,我可以访问该错误消息吗?谢谢
尝试覆盖django.conf.urls.defaults.handler500
您的urls.py
。
from django.conf.urls.defaults import * handler500 = 'path.to.my_custom_500'
甚至更好-编写您自己的处理程序并将其放入LOGGING
设置中。
编辑:
您还可以在my_custom_500
代码中添加识别异常类型的代码,例如:
import sys; def my_custom_500(request): ... type_, value, traceback = sys.exc_info() ...