Skip to content

Commit c27261b

Browse files
Don't raise W006 warning when app loader is specified. (#1556)
* Don't raise W006 warning when app loader is specified. Fixes #1550 * Update docs/changes.rst Co-authored-by: Matthias Kestenholz <mk@feinheit.ch>
1 parent 66fe9c8 commit c27261b

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

debug_toolbar/apps.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ def ready(self):
2222
DebugToolbar.get_panel_classes()
2323

2424

25+
def check_template_config(config):
26+
"""
27+
Checks if a template configuration is valid.
28+
29+
The toolbar requires either the toolbars to be unspecified or
30+
``django.template.loaders.app_directories.Loader`` to be
31+
included in the loaders.
32+
If custom loaders are specified, then APP_DIRS must be True.
33+
"""
34+
app_dirs = config.get("APP_DIRS", False)
35+
loaders = config.get("OPTIONS", {}).get("loaders", None)
36+
# By default the app loader is included.
37+
has_app_loaders = (
38+
loaders is None or "django.template.loaders.app_directories.Loader" in loaders
39+
)
40+
return has_app_loaders or app_dirs
41+
42+
2543
@register
2644
def check_middleware(app_configs, **kwargs):
2745
from debug_toolbar.middleware import DebugToolbarMiddleware
@@ -30,13 +48,16 @@ def check_middleware(app_configs, **kwargs):
3048
gzip_index = None
3149
debug_toolbar_indexes = []
3250

33-
if all(not config.get("APP_DIRS", False) for config in settings.TEMPLATES):
51+
if all(not check_template_config(config) for config in settings.TEMPLATES):
3452
errors.append(
3553
Warning(
3654
"At least one DjangoTemplates TEMPLATES configuration needs "
37-
"to have APP_DIRS set to True.",
55+
"to use django.template.loaders.app_directories.Loader or "
56+
"have APP_DIRS set to True.",
3857
hint=(
39-
"Use APP_DIRS=True for at least one "
58+
"Include django.template.loaders.app_directories.Loader "
59+
'in ["OPTIONS"]["loaders"]. Alternatively use '
60+
"APP_DIRS=True for at least one "
4061
"django.template.backends.django.DjangoTemplates "
4162
"backend configuration."
4263
),

docs/changes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Next version
55
------------
66

77
* Removed support for Django < 3.2.
8+
* Updated check ``W006`` to look for
9+
``django.template.loaders.app_directories.Loader``.
810

911
3.2.4 (2021-12-15)
1012
------------------

docs/checks.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ Debug Toolbar setup and configuration:
1515
``MIDDLEWARE_CLASSES`` setting.
1616
* **debug_toolbar.W005**: Setting ``DEBUG_TOOLBAR_PANELS`` is empty.
1717
* **debug_toolbar.W006**: At least one ``DjangoTemplates`` ``TEMPLATES``
18-
configuration needs to have ``APP_DIRS`` set to ``True``.
18+
configuration needs to have
19+
``django.template.loaders.app_directories.Loader`` included in
20+
``["OPTIONS"]["loaders"]`` or ``APP_DIRS`` set to ``True``.

tests/test_checks.py

+47-5
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,67 @@ def test_panels_is_empty(self):
134134
"django.template.context_processors.request",
135135
"django.contrib.auth.context_processors.auth",
136136
"django.contrib.messages.context_processors.messages",
137-
]
137+
],
138+
"loaders": [
139+
"django.template.loaders.filesystem.Loader",
140+
],
138141
},
139142
},
140143
]
141144
)
142-
def test_templates_is_using_app_dirs_false(self):
145+
def test_check_w006_invalid(self):
143146
errors = run_checks()
144147
self.assertEqual(
145148
errors,
146149
[
147150
Warning(
148-
"At least one DjangoTemplates TEMPLATES configuration "
149-
"needs to have APP_DIRS set to True.",
151+
"At least one DjangoTemplates TEMPLATES configuration needs "
152+
"to use django.template.loaders.app_directories.Loader or "
153+
"have APP_DIRS set to True.",
150154
hint=(
151-
"Use APP_DIRS=True for at least one "
155+
"Include django.template.loaders.app_directories.Loader "
156+
'in ["OPTIONS"]["loaders"]. Alternatively use '
157+
"APP_DIRS=True for at least one "
152158
"django.template.backends.django.DjangoTemplates "
153159
"backend configuration."
154160
),
155161
id="debug_toolbar.W006",
156162
)
157163
],
158164
)
165+
166+
@override_settings(
167+
TEMPLATES=[
168+
{
169+
"NAME": "use_loaders",
170+
"BACKEND": "django.template.backends.django.DjangoTemplates",
171+
"APP_DIRS": False,
172+
"OPTIONS": {
173+
"context_processors": [
174+
"django.template.context_processors.debug",
175+
"django.template.context_processors.request",
176+
"django.contrib.auth.context_processors.auth",
177+
"django.contrib.messages.context_processors.messages",
178+
],
179+
"loaders": [
180+
"django.template.loaders.app_directories.Loader",
181+
],
182+
},
183+
},
184+
{
185+
"NAME": "use_app_dirs",
186+
"BACKEND": "django.template.backends.django.DjangoTemplates",
187+
"APP_DIRS": True,
188+
"OPTIONS": {
189+
"context_processors": [
190+
"django.template.context_processors.debug",
191+
"django.template.context_processors.request",
192+
"django.contrib.auth.context_processors.auth",
193+
"django.contrib.messages.context_processors.messages",
194+
],
195+
},
196+
},
197+
]
198+
)
199+
def test_check_w006_valid(self):
200+
self.assertEqual(run_checks(), [])

0 commit comments

Comments
 (0)