Skip to content

Commit d7a1124

Browse files
committed
Cache merged URL configurations by their original name.
This accomodates sites where middleware may change the urlconf for two different request.
1 parent e08ec8c commit d7a1124

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

debug_toolbar/middleware.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def get_current(cls):
3838
return cls.debug_toolbars.get(thread.get_ident())
3939

4040
def __init__(self):
41-
self.override_url = True
42-
41+
self._urlconfs = {}
42+
4343
# Set method to use to decide to show toolbar
4444
self.show_toolbar = self._show_toolbar # default
4545

@@ -72,17 +72,27 @@ def _show_toolbar(self, request):
7272
def process_request(self, request):
7373
__traceback_hide__ = True
7474
if self.show_toolbar(request):
75-
if self.override_url:
75+
76+
urlconf_name = getattr(request, 'urlconf', settings.ROOT_URLCONF)
77+
if urlconf_name not in self._urlconfs:
78+
79+
import imp, copy
80+
7681
original_urlconf = __import__(getattr(request, 'urlconf', settings.ROOT_URLCONF), {}, {}, ['*'])
77-
debug_toolbar.urls.urlpatterns += patterns('',
82+
new_urlconf = imp.new_module('urlconf')
83+
new_urlconf.urlpatterns = copy.copy(debug_toolbar.urls.urlpatterns)
84+
85+
new_urlconf.urlpatterns += patterns('',
7886
('', include(original_urlconf)),
7987
)
8088
if hasattr(original_urlconf, 'handler404'):
81-
debug_toolbar.urls.handler404 = original_urlconf.handler404
89+
new_urlconf.handler404 = original_urlconf.handler404
8290
if hasattr(original_urlconf, 'handler500'):
83-
debug_toolbar.urls.handler500 = original_urlconf.handler500
84-
self.override_url = False
85-
request.urlconf = 'debug_toolbar.urls'
91+
new_urlconf.handler500 = original_urlconf.handler500
92+
93+
self._urlconfs[urlconf_name] = new_urlconf
94+
95+
request.urlconf = self._urlconfs[urlconf_name]
8696

8797
toolbar = DebugToolbar(request)
8898
for panel in toolbar.panels:

0 commit comments

Comments
 (0)