在自定义500处理程序中捕获错误文本

 北路新华 发布于 2023-01-06 19:20

在我的某个地方,我抛出了一个错误,尤其是这个错误:

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)

无论如何,我可以访问该错误消息吗?谢谢

1 个回答
  • 尝试覆盖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()
        ...
    

    2023-01-06 19:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有