有没有办法向管理员显示Django调试堆栈跟踪页面,即使设置中DEBUG = False?

问题描述:

情景:而不是我不得不SSH到一个盒子来检索一个堆栈跟踪,我更喜欢非技术的同事只是给我发送错误!

Scenario: instead of me having to SSH into a box to retrieve a stacktrace, I'd prefer non-techie co-workers to just email me the error!

Django有没有办法或钩子来做这样的事情?例如

Is there a way or hook in Django to do something like this? e.g.

def 500_error_happened(request):    # psuedocode >__<
    if request.user.is_staff:
        show_the_debug_stack_trace_page()
    else:
        show_user_friendly_500_html_page()


找到答案!您可以使用显示 technical_500_response 的超级用户的中间件:

found an answer! You can use a middleware which displays technical_500_response to superusers:

from django.views.debug import technical_500_response
import sys

class UserBasedExceptionMiddleware(object):
    def process_exception(self, request, exception):
        if request.user.is_superuser:
            return technical_500_response(request, *sys.exc_info())

(from https://djangosnippets.org/snippets/935/