gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:10:22 24 4
gpt4 key购买 nike

它是以下内容的副本: 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("/")

关于python - 当 debug = False 时只为管理员打开 django 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19077743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com