Skip to content

Commit 1ea7c95

Browse files
authored
Fix #2109: Recursively unwrap loaders to support template partials (#2117)
1 parent 11e28f7 commit 1ea7c95

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

debug_toolbar/panels/templates/views.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ def template_source(request):
2727
template_name = request.GET.get("template", template_origin_name)
2828

2929
final_loaders = []
30-
loaders = Engine.get_default().template_loaders
30+
loaders = list(Engine.get_default().template_loaders)
31+
32+
while loaders:
33+
loader = loaders.pop(0)
3134

32-
for loader in loaders:
3335
if loader is not None:
34-
# When the loader has loaders associated with it,
35-
# append those loaders to the list. This occurs with
36-
# django.template.loaders.cached.Loader
36+
# Recursively unwrap loaders until we get to loaders which do not
37+
# themselves wrap other loaders. This adds support for
38+
# django.template.loaders.cached.Loader and the
39+
# django-template-partials loader (possibly among others)
3740
if hasattr(loader, "loaders"):
38-
final_loaders += loader.loaders
41+
loaders.extend(loader.loaders)
3942
else:
4043
final_loaders.append(loader)
4144

docs/changes.rst

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Pending
77
* Added hook to RedirectsPanel for subclass customization.
88
* Added feature to sanitize sensitive data in the Request Panel.
99
* Fixed dark mode conflict in code block toolbar CSS
10+
* Added support for using django-template-partials with the template panel's
11+
source view functionality. The same change possibly adds support for other
12+
template loaders.
1013

1114
5.1.0 (2025-03-20)
1215
------------------

tests/panels/test_template.py

+18
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ def test_lazyobject_eval(self):
132132
self.panel.generate_stats(self.request, response)
133133
self.assertIn("lazy_value", self.panel.content)
134134

135+
@override_settings(
136+
DEBUG=True,
137+
DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.templates.TemplatesPanel"],
138+
)
139+
def test_template_source(self):
140+
from django.core import signing
141+
from django.template.loader import get_template
142+
143+
template = get_template("basic.html")
144+
url = "/__debug__/template_source/"
145+
data = {
146+
"template": template.template.name,
147+
"template_origin": signing.dumps(template.template.origin.name),
148+
}
149+
150+
response = self.client.get(url, data)
151+
self.assertEqual(response.status_code, 200)
152+
135153

136154
@override_settings(
137155
DEBUG=True, DEBUG_TOOLBAR_PANELS=["debug_toolbar.panels.templates.TemplatesPanel"]

tests/settings.py

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# Quick-start development settings - unsuitable for production
99

10+
DEBUG = False
1011
SECRET_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
1112

1213
INTERNAL_IPS = ["127.0.0.1"]
@@ -27,6 +28,10 @@
2728
"django.contrib.messages",
2829
"django.contrib.staticfiles",
2930
"debug_toolbar",
31+
# We are not actively using template-partials; we just want more nesting
32+
# in our template loader configuration, see
33+
# https://github.com/django-commons/django-debug-toolbar/issues/2109
34+
"template_partials",
3035
"tests",
3136
]
3237

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ deps =
2626
selenium>=4.8.0
2727
sqlparse
2828
django-csp
29+
django-template-partials
2930
passenv=
3031
CI
3132
COVERAGE_ARGS

0 commit comments

Comments
 (0)