diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index 69100b20d..e1cf32d23 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -44,7 +44,7 @@ def __init__(self, get_response): def __call__(self, request): # Decide whether the toolbar is active for this request. show_toolbar = get_show_toolbar() - if not show_toolbar(request) or request.path.startswith("/__debug__/"): + if not show_toolbar(request) or DebugToolbar.is_toolbar_request(request): return self.get_response(request) toolbar = DebugToolbar(request, self.get_response) diff --git a/debug_toolbar/toolbar.py b/debug_toolbar/toolbar.py index 9638ba1f8..fd82d62e2 100644 --- a/debug_toolbar/toolbar.py +++ b/debug_toolbar/toolbar.py @@ -9,7 +9,8 @@ from django.core.exceptions import ImproperlyConfigured from django.template import TemplateSyntaxError from django.template.loader import render_to_string -from django.urls import path +from django.urls import path, resolve +from django.urls.exceptions import Resolver404 from django.utils.module_loading import import_string from debug_toolbar import settings as dt_settings @@ -133,6 +134,19 @@ def get_urls(cls): cls._urlpatterns = urlpatterns return cls._urlpatterns + @classmethod + def is_toolbar_request(cls, request): + """ + Determine if the request is for a DebugToolbar view. + """ + # The primary caller of this function is in the middleware which may + # not have resolver_match set. + try: + resolver_match = request.resolver_match or resolve(request.path) + except Resolver404: + return False + return resolver_match.namespaces and resolver_match.namespaces[-1] == app_name + app_name = "djdt" urlpatterns = DebugToolbar.get_urls() diff --git a/docs/changes.rst b/docs/changes.rst index 6207b6797..bef5d1964 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -9,7 +9,8 @@ Next version * Added ``PRETTIFY_SQL`` configuration option to support controlling SQL token grouping. By default it's set to True. When set to False, a performance improvement can be seen by the SQL panel. - +* Fixed issue with toolbar expecting URL paths to start with `/__debug__/` + while the documentation indicates it's not required. 3.2 (2020-12-03) ---------------- diff --git a/tests/test_integration.py b/tests/test_integration.py index 8c28f6c6e..59ea1c25f 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -101,6 +101,25 @@ def test_cache_page(self): self.client.get("/cached_view/") self.assertEqual(len(self.toolbar.get_panel_by_id("CachePanel").calls), 5) + def test_is_toolbar_request(self): + self.request.path = "/__debug__/render_panel/" + self.assertTrue(self.toolbar.is_toolbar_request(self.request)) + + self.request.path = "/invalid/__debug__/render_panel/" + self.assertFalse(self.toolbar.is_toolbar_request(self.request)) + + self.request.path = "/render_panel/" + self.assertFalse(self.toolbar.is_toolbar_request(self.request)) + + @override_settings(ROOT_URLCONF="tests.urls_invalid") + def test_is_toolbar_request_without_djdt_urls(self): + """Test cases when the toolbar urls aren't configured.""" + self.request.path = "/__debug__/render_panel/" + self.assertFalse(self.toolbar.is_toolbar_request(self.request)) + + self.request.path = "/render_panel/" + self.assertFalse(self.toolbar.is_toolbar_request(self.request)) + @override_settings(DEBUG=True) class DebugToolbarIntegrationTestCase(IntegrationTestCase): diff --git a/tests/urls_invalid.py b/tests/urls_invalid.py new file mode 100644 index 000000000..ccadb6735 --- /dev/null +++ b/tests/urls_invalid.py @@ -0,0 +1,2 @@ +"""Invalid urls.py file for testing""" +urlpatterns = []