Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch on django error page only for admins when debug = False

Tags:

python

django

It is a copy of: is there a way to show the Django debug stacktrace page to admin users even if DEBUG=False in settings? but there is no answer

How to show django error page with stacktrace when debug=False only for admin users. I don't want to use sentry.

like image 206
124bit Avatar asked Feb 15 '26 21:02

124bit


1 Answers

I find an answer

In urls.py add

handler404 = 'site_utils.handler404'

Create site_utils.py in the same folder where urls.py and add there

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("/")
like image 131
124bit Avatar answered Feb 18 '26 18:02

124bit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!