Skip to content

Commit

Permalink
Fixed #1780 -- Adjusted system check to allow for nested template loa…
Browse files Browse the repository at this point in the history
…ders.

Django's cached loader wraps a list of child loaders, which may
correctly contain the required app directories loader.
  • Loading branch information
carltongibson authored and tim-schilling committed May 16, 2023
1 parent de7e19c commit 202c301
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions debug_toolbar/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,26 @@ def check_template_config(config):
included in the loaders.
If custom loaders are specified, then APP_DIRS must be True.
"""

def flat_loaders(loaders):
"""
Recursively flatten the settings list of template loaders.
Check for (loader, [child_loaders]) tuples.
Django's default cached loader uses this pattern.
"""
for loader in loaders:
if isinstance(loader, tuple):
yield loader[0]
yield from flat_loaders(loader[1])
else:
yield loader

app_dirs = config.get("APP_DIRS", False)
loaders = config.get("OPTIONS", {}).get("loaders", None)
if loaders:
loaders = list(flat_loaders(loaders))

# By default the app loader is included.
has_app_loaders = (
loaders is None or "django.template.loaders.app_directories.Loader" in loaders
Expand Down
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Change log
Pending
-------

* Adjusted app directories system check to allow for nested template loaders.

4.1.0 (2023-05-15)
------------------

Expand Down
29 changes: 29 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,32 @@ def test_check_w006_invalid(self):
)
def test_check_w006_valid(self):
self.assertEqual(run_checks(), [])

@override_settings(
TEMPLATES=[
{
"NAME": "use_loaders",
"BACKEND": "django.template.backends.django.DjangoTemplates",
"APP_DIRS": False,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
"loaders": [
(
"django.template.loaders.cached.Loader",
[
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
),
],
},
},
]
)
def test_check_w006_valid_nested_loaders(self):
self.assertEqual(run_checks(), [])

0 comments on commit 202c301

Please sign in to comment.