File tree Expand file tree Collapse file tree 5 files changed +39
-3
lines changed Expand file tree Collapse file tree 5 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ def __init__(self, get_response):
44
44
def __call__ (self , request ):
45
45
# Decide whether the toolbar is active for this request.
46
46
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 ):
48
48
return self .get_response (request )
49
49
50
50
toolbar = DebugToolbar (request , self .get_response )
Original file line number Diff line number Diff line change 9
9
from django .core .exceptions import ImproperlyConfigured
10
10
from django .template import TemplateSyntaxError
11
11
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
13
14
from django .utils .module_loading import import_string
14
15
15
16
from debug_toolbar import settings as dt_settings
@@ -133,6 +134,19 @@ def get_urls(cls):
133
134
cls ._urlpatterns = urlpatterns
134
135
return cls ._urlpatterns
135
136
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
+
136
150
137
151
app_name = "djdt"
138
152
urlpatterns = DebugToolbar .get_urls ()
Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ Next version
9
9
* Added ``PRETTIFY_SQL `` configuration option to support controlling
10
10
SQL token grouping. By default it's set to True. When set to False,
11
11
a performance improvement can be seen by the SQL panel.
12
-
12
+ * Fixed issue with toolbar expecting URL paths to start with `/__debug__/ `
13
+ while the documentation indicates it's not required.
13
14
14
15
3.2 (2020-12-03)
15
16
----------------
Original file line number Diff line number Diff line change @@ -101,6 +101,25 @@ def test_cache_page(self):
101
101
self .client .get ("/cached_view/" )
102
102
self .assertEqual (len (self .toolbar .get_panel_by_id ("CachePanel" ).calls ), 5 )
103
103
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
+
104
123
105
124
@override_settings (DEBUG = True )
106
125
class DebugToolbarIntegrationTestCase (IntegrationTestCase ):
Original file line number Diff line number Diff line change
1
+ """Invalid urls.py file for testing"""
2
+ urlpatterns = []
You can’t perform that action at this time.
0 commit comments