Skip to main content
 首页 » 编程设计

python之当 debug = False 时只为管理员打开 django 错误页面

2025年05月04日39zhengyun_ustc

它是以下内容的副本: is there a way to show the Django debug stacktrace page to admin users even if DEBUG=False in settings? 但没有答案

如何在 debug=False 时仅对管理员用户显示带有堆栈跟踪的 django 错误页面。 我不想使用哨兵。

请您参考如下方法:

找到答案

在urls.py中添加

handler404 = 'site_utils.handler404' 

在与 urls.py 相同的文件夹中创建 site_utils.py 并添加到那里

from django.http import HttpResponseRedirect,  HttpResponsePermanentRedirect 
import sys 
from django.views.debug import technical_404_response, technical_500_response  
def handler404(request): 
    if (request.user.is_active and request.user.is_staff) or request.user.is_superuser: 
        exc_type, exc_value, tb = sys.exc_info() 
        return technical_404_response(request, exc_value) 
    else: 
        return  HttpResponsePermanentRedirect("/")