From b66c950d4cb8672a924f9652224772d7219abbe2 Mon Sep 17 00:00:00 2001 From: Rob Hudson Date: Thu, 2 Oct 2008 20:45:18 -0700 Subject: A fix for URL patterns getting appended on each request growing indefinitely. This may break with other middleware patching urlpatterns, though. --- debug_toolbar/middleware.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'debug_toolbar/middleware.py') diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index 140ceba..1b2eb4d 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -22,6 +22,9 @@ class DebugToolbarMiddleware(object): """ def __init__(self): self.debug_toolbar = None + self.original_urlconf = settings.ROOT_URLCONF + self.original_pattern = patterns('', ('', include(self.original_urlconf)),) + self.override_url = True def show_toolbar(self, request): if not settings.DEBUG: @@ -33,15 +36,9 @@ class DebugToolbarMiddleware(object): return True def process_request(self, request): - # Monkeypatch in the URLpatterns for the debug toolbar. The last item - # in the URLpatterns needs to be ```('', include(ROOT_URLCONF))``` so - # that the existing URLs load *after* the ones we patch in. However, - # this is difficult to get right: a previous middleware might have - # changed request.urlconf, so we need to pick that up instead. - original_urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF) - debug_toolbar.urls.urlpatterns += patterns('', - ('', include(original_urlconf)), - ) + if self.override_url: + debug_toolbar.urls.urlpatterns += self.original_pattern + self.override_url = False request.urlconf = 'debug_toolbar.urls' if self.show_toolbar(request): -- cgit v1.2.3