Skip to content

Commit f4c263a

Browse files
Support for request-level urlconf overrides (#1488)
* Support for request-level urlconf overrides Middlewares can override the default urlconf per request by setting a "urlconf" attribute to the inbound HttpRequest instance (doc: https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.HttpRequest.urlconf ) This change adds support for this kind of override * Add test confirming urlconf override functionality. Update docs for urlconf override per request. Co-authored-by: tschilling <schillingt@better-simple.com>
1 parent 15a581d commit f4c263a

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

debug_toolbar/toolbar.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def is_toolbar_request(cls, request):
146146
# The primary caller of this function is in the middleware which may
147147
# not have resolver_match set.
148148
try:
149-
resolver_match = request.resolver_match or resolve(request.path)
149+
resolver_match = request.resolver_match or resolve(
150+
request.path, getattr(request, "urlconf", None)
151+
)
150152
except Resolver404:
151153
return False
152154
return resolver_match.namespaces and resolver_match.namespaces[-1] == app_name

docs/changes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Next version
1111
* Fixed ``RENDER_PANELS`` functionality so that when ``True`` panels are
1212
rendered during the request and not loaded asynchronously.
1313
* HistoryPanel now shows status codes of responses.
14+
* Support ``request.urlconf`` override when checking for toolbar requests.
1415

1516

1617
3.2.1 (2021-04-14)

tests/test_integration.py

+11
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ def test_is_toolbar_request_without_djdt_urls(self):
121121
self.request.path = "/render_panel/"
122122
self.assertFalse(self.toolbar.is_toolbar_request(self.request))
123123

124+
@override_settings(ROOT_URLCONF="tests.urls_invalid")
125+
def test_is_toolbar_request_override_request_urlconf(self):
126+
"""Test cases when the toolbar URL is configured on the request."""
127+
self.request.path = "/__debug__/render_panel/"
128+
self.assertFalse(self.toolbar.is_toolbar_request(self.request))
129+
130+
# Verify overriding the urlconf on the request is valid.
131+
self.request.urlconf = "tests.urls"
132+
self.request.path = "/__debug__/render_panel/"
133+
self.assertTrue(self.toolbar.is_toolbar_request(self.request))
134+
124135

125136
@override_settings(DEBUG=True)
126137
class DebugToolbarIntegrationTestCase(IntegrationTestCase):

0 commit comments

Comments
 (0)