如何获取纯文本Django错误页面

发布于 2021-01-29 18:07:17

在开发过程中,我以Debug模式运行Django,并使用文本模式应用程序将数据发布到我的应用程序中。理想情况下,当我收到http错误代码500时,我需要接收纯文本响应,因此我不必在所有HTML和Javascript中寻找真正的错误。

是否可以获取纯文本的Django 500 Internal Server Error?

关注者
0
被浏览
45
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    我想写一个中间件,因为否则该异常在500.html中不可用

    http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-
    exception

    class ProcessExceptionMiddleware(object):
        def process_exception(self, request, exception):
            t = Template("500 Error: {{ exception }}")
            response_html = t.render(Context({'exception' : exception }))
    
            response = http.HttpResponse(response_html)
            response.status_code = 500
            return response
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看