Skip to content

Commit 202c301

Browse files
carltongibsontim-schilling
authored andcommitted
Fixed #1780 -- Adjusted system check to allow for nested template loaders.
Django's cached loader wraps a list of child loaders, which may correctly contain the required app directories loader.
1 parent de7e19c commit 202c301

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

debug_toolbar/apps.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,26 @@ def check_template_config(config):
3232
included in the loaders.
3333
If custom loaders are specified, then APP_DIRS must be True.
3434
"""
35+
36+
def flat_loaders(loaders):
37+
"""
38+
Recursively flatten the settings list of template loaders.
39+
40+
Check for (loader, [child_loaders]) tuples.
41+
Django's default cached loader uses this pattern.
42+
"""
43+
for loader in loaders:
44+
if isinstance(loader, tuple):
45+
yield loader[0]
46+
yield from flat_loaders(loader[1])
47+
else:
48+
yield loader
49+
3550
app_dirs = config.get("APP_DIRS", False)
3651
loaders = config.get("OPTIONS", {}).get("loaders", None)
52+
if loaders:
53+
loaders = list(flat_loaders(loaders))
54+
3755
# By default the app loader is included.
3856
has_app_loaders = (
3957
loaders is None or "django.template.loaders.app_directories.Loader" in loaders

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Change log
44
Pending
55
-------
66

7+
* Adjusted app directories system check to allow for nested template loaders.
8+
79
4.1.0 (2023-05-15)
810
------------------
911

tests/test_checks.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,32 @@ def test_check_w006_invalid(self):
198198
)
199199
def test_check_w006_valid(self):
200200
self.assertEqual(run_checks(), [])
201+
202+
@override_settings(
203+
TEMPLATES=[
204+
{
205+
"NAME": "use_loaders",
206+
"BACKEND": "django.template.backends.django.DjangoTemplates",
207+
"APP_DIRS": False,
208+
"OPTIONS": {
209+
"context_processors": [
210+
"django.template.context_processors.debug",
211+
"django.template.context_processors.request",
212+
"django.contrib.auth.context_processors.auth",
213+
"django.contrib.messages.context_processors.messages",
214+
],
215+
"loaders": [
216+
(
217+
"django.template.loaders.cached.Loader",
218+
[
219+
"django.template.loaders.filesystem.Loader",
220+
"django.template.loaders.app_directories.Loader",
221+
],
222+
),
223+
],
224+
},
225+
},
226+
]
227+
)
228+
def test_check_w006_valid_nested_loaders(self):
229+
self.assertEqual(run_checks(), [])

0 commit comments

Comments
 (0)