【问题标题】:Django exception middleware: simultaneously show traceback in browser and local consoleDjango异常中间件:同时在浏览器和本地控制台中显示回溯
【发布时间】:2020-04-11 09:43:11
【问题描述】:

在开发过程中,我设法使用一些自定义中间件将回溯消息(默认显示在本地控制台中)重定向到客户端的浏览器控制台。

但是,执行此操作时,回溯不再显示在控制台中。有没有办法让它在本地控制台中,在客户端的浏览器中?

使用的中间件(基于herehere 的解决方案):

import traceback 
from django.http import HttpResponse

class ErrorMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        return self.get_response(request)

    def process_exception(self, request, exception):
        tb = traceback.format_exc()
        return HttpResponse(tb)

【问题讨论】:

    标签: django traceback django-middleware


    【解决方案1】:

    process_exception 返回一个HttpResponse 将导致其他 middleware.process_exception 无法运行。

    https://docs.djangoproject.com/en/3.0/topics/http/middleware/#process-template-response

    默认情况下,如果为DEBUG = True,则堆栈跟踪应同时显示在开发服务器控制台和浏览器中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-26
      • 2023-04-02
      • 2020-04-20
      • 2021-08-26
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多