Skip to content

Instantly share code, notes, and snippets.

@erthalion
Last active December 19, 2015 11:29
Show Gist options
  • Save erthalion/5947808 to your computer and use it in GitHub Desktop.
Save erthalion/5947808 to your computer and use it in GitHub Desktop.
Django debug_toolbar vs handle403
# django.core.handlers.base.py
def get_response(self, request):
"Returns an HttpResponse object for the given HttpRequest"
# debug_toolbar add own middleware that set urlconf for request (it's module)
# so handler403 doesn't work
response = None
# Apply request middleware
for middleware_method in self._request_middleware:
response = middleware_method(request)
if response:
break
# debug_toolbar middleware
def process_request(self, request):
__traceback_hide__ = True
if self.show_toolbar(request):
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
if isinstance(urlconf, basestring):
urlconf = import_module(getattr(request, 'urlconf', settings.ROOT_URLCONF))
if urlconf not in self._urlconfs:
new_urlconf = imp.new_module('urlconf')
new_urlconf.urlpatterns = debug_toolbar.urls.urlpatterns + \
list(urlconf.urlpatterns)
if hasattr(urlconf, 'handler403'):
new_urlconf.handler403 = urlconf.handler403
if hasattr(urlconf, 'handler404'):
new_urlconf.handler404 = urlconf.handler404
if hasattr(urlconf, 'handler500'):
new_urlconf.handler500 = urlconf.handler500
self._urlconfs[urlconf] = new_urlconf
request.urlconf = self._urlconfs[urlconf]
toolbar = DebugToolbar(request)
for panel in toolbar.panels:
panel.process_request(request)
self.__class__.debug_toolbars[threading.currentThread().ident] = toolbar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment