Skip to content

Commit d07c2d5

Browse files
committed
Fixed issue with toolbar expecting urls to start with __debug__.
When the history panel was added and we expanded the number of urls we were instrumenting, there was a check added to exclude the requests to the toolbar's views. This was done by checking the path of the request to see if it started with __debug__. However, the documentation states that is not a requirement. This change checks the urls to see if the namespace of the resolved url matches the toolbar's namespace. This still isn't perfect as another url path could contain the last namespace of djdt, but this is much less likely than a user defining the toolbar's urls at a different path than /__debug__/
1 parent 3dc5437 commit d07c2d5

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

debug_toolbar/middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, get_response):
4444
def __call__(self, request):
4545
# Decide whether the toolbar is active for this request.
4646
show_toolbar = get_show_toolbar()
47-
if not show_toolbar(request) or request.path.startswith("/__debug__/"):
47+
if not show_toolbar(request) or DebugToolbar.is_toolbar_request(request):
4848
return self.get_response(request)
4949

5050
toolbar = DebugToolbar(request, self.get_response)

debug_toolbar/toolbar.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from django.core.exceptions import ImproperlyConfigured
1010
from django.template import TemplateSyntaxError
1111
from django.template.loader import render_to_string
12-
from django.urls import path
12+
from django.urls import path, resolve
13+
from django.urls.exceptions import Resolver404
1314
from django.utils.module_loading import import_string
1415

1516
from debug_toolbar import settings as dt_settings
@@ -133,6 +134,19 @@ def get_urls(cls):
133134
cls._urlpatterns = urlpatterns
134135
return cls._urlpatterns
135136

137+
@classmethod
138+
def is_toolbar_request(cls, request):
139+
"""
140+
Determine if the request is for a DebugToolbar view.
141+
"""
142+
# The primary caller of this function is in the middleware which may
143+
# not have resolver_match set.
144+
try:
145+
resolver_match = request.resolver_match or resolve(request.path)
146+
except Resolver404:
147+
return False
148+
return resolver_match.namespaces and resolver_match.namespaces[-1] == app_name
149+
136150

137151
app_name = "djdt"
138152
urlpatterns = DebugToolbar.get_urls()

docs/changes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Next version
99
* Added ``PRETTIFY_SQL`` configuration option to support controlling
1010
SQL token grouping. By default it's set to True. When set to False,
1111
a performance improvement can be seen by the SQL panel.
12-
12+
* Fixed issue with toolbar expecting urls to start with `/__debug__/`
13+
while the documentation indicates it's not required.
1314

1415
3.2 (2020-12-03)
1516
----------------

tests/test_integration.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ def test_cache_page(self):
101101
self.client.get("/cached_view/")
102102
self.assertEqual(len(self.toolbar.get_panel_by_id("CachePanel").calls), 5)
103103

104+
def test_is_toolbar_request(self):
105+
self.request.path = "/__debug__/render_panel/"
106+
self.assertTrue(self.toolbar.is_toolbar_request(self.request))
107+
108+
self.request.path = "/invalid/__debug__/render_panel/"
109+
self.assertFalse(self.toolbar.is_toolbar_request(self.request))
110+
111+
self.request.path = "/render_panel/"
112+
self.assertFalse(self.toolbar.is_toolbar_request(self.request))
113+
114+
@override_settings(ROOT_URLCONF="tests.urls_invalid")
115+
def test_is_toolbar_request_without_djdt_urls(self):
116+
"""Test cases when the toolbar urls aren't configured."""
117+
self.request.path = "/__debug__/render_panel/"
118+
self.assertFalse(self.toolbar.is_toolbar_request(self.request))
119+
120+
self.request.path = "/render_panel/"
121+
self.assertFalse(self.toolbar.is_toolbar_request(self.request))
122+
104123

105124
@override_settings(DEBUG=True)
106125
class DebugToolbarIntegrationTestCase(IntegrationTestCase):

tests/urls_invalid.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"""Invalid urls.py file for testing"""
2+
urlpatterns = []

0 commit comments

Comments
 (0)