Skip to content

Fixed issue with toolbar expecting urls to start with __debug__. #1453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion debug_toolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
----------------
Expand Down
19 changes: 19 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/urls_invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""Invalid urls.py file for testing"""
urlpatterns = []