Skip to content

Commit 277fe2e

Browse files
authored
Fixed #25788 -- Enabled the cached template loader if debug is False.
1 parent 2ced2f7 commit 277fe2e

File tree

6 files changed

+55
-13
lines changed

6 files changed

+55
-13
lines changed

django/template/engine.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def __init__(self, dirs=None, app_dirs=False, context_processors=None,
2727
loaders = ['django.template.loaders.filesystem.Loader']
2828
if app_dirs:
2929
loaders += ['django.template.loaders.app_directories.Loader']
30+
if not debug:
31+
loaders = [('django.template.loaders.cached.Loader', loaders)]
3032
else:
3133
if app_dirs:
3234
raise ImproperlyConfigured(

docs/ref/templates/api.txt

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,16 @@ what's passed by :class:`~django.template.backends.django.DjangoTemplates`.
103103
* ``'django.template.loaders.app_directories.Loader'`` if and only if
104104
``app_dirs`` is ``True``.
105105

106+
If ``debug`` is ``False``, these loaders are wrapped in
107+
:class:`django.template.loaders.cached.Loader`.
108+
106109
See :ref:`template-loaders` for details.
107110

111+
.. versionchanged:: 1.11
112+
113+
Enabling of the cached template loader when ``debug`` is ``False``
114+
was added.
115+
108116
* ``string_if_invalid`` is the output, as a string, that the template
109117
system should use for invalid (e.g. misspelled) variables.
110118

@@ -899,18 +907,22 @@ loaders that come with Django:
899907

900908
.. class:: cached.Loader
901909

902-
By default, the templating system will read and compile your templates every
903-
time they need to be rendered. While the Django templating system is quite
904-
fast, the overhead from reading and compiling templates can add up.
910+
By default (when :setting:`DEBUG` is ``True``), the template system reads
911+
and compiles your templates every time they're rendered. While the Django
912+
template system is quite fast, the overhead from reading and compiling
913+
templates can add up.
905914

906-
The cached template loader is a class-based loader that you configure with
907-
a list of other loaders that it should wrap. The wrapped loaders are used to
908-
locate unknown templates when they are first encountered. The cached loader
909-
then stores the compiled ``Template`` in memory. The cached ``Template``
910-
instance is returned for subsequent requests to load the same template.
915+
You configure the cached template loader with a list of other loaders that
916+
it should wrap. The wrapped loaders are used to locate unknown templates
917+
when they're first encountered. The cached loader then stores the compiled
918+
``Template`` in memory. The cached ``Template`` instance is returned for
919+
subsequent requests to load the same template.
911920

912-
For example, to enable template caching with the ``filesystem`` and
913-
``app_directories`` template loaders you might use the following settings::
921+
This loader is automatically enabled if :setting:`DEBUG` is ``False`` and
922+
:setting:`OPTIONS['loaders'] <TEMPLATES-OPTIONS>` isn't specified.
923+
924+
You can also enable template caching with some custom template loaders
925+
using settings like this::
914926

915927
TEMPLATES = [{
916928
'BACKEND': 'django.template.backends.django.DjangoTemplates',
@@ -920,6 +932,7 @@ loaders that come with Django:
920932
('django.template.loaders.cached.Loader', [
921933
'django.template.loaders.filesystem.Loader',
922934
'django.template.loaders.app_directories.Loader',
935+
'path.to.custom.Loader',
923936
]),
924937
],
925938
},
@@ -934,7 +947,10 @@ loaders that come with Django:
934947
information, see :ref:`template tag thread safety considerations
935948
<template_tag_thread_safety>`.
936949

937-
This loader is disabled by default.
950+
.. versionchanged:: 1.11
951+
952+
The automatic enabling of the cached template loader when ``debug`` is
953+
``False`` was added.
938954

939955
``django.template.loaders.locmem.Loader``
940956

docs/releases/1.11.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,12 @@ Miscellaneous
471471
:class:`~django.core.serializers.json.DjangoJSONEncoder` (renamed in Django
472472
1.0) is removed.
473473

474+
* The :class:`cached template loader <django.template.loaders.cached.Loader>`
475+
is now enabled if :setting:`DEBUG` is ``False`` and
476+
:setting:`OPTIONS['loaders'] <TEMPLATES-OPTIONS>` isn't specified. This could
477+
be backwards-incompatible if you have some :ref:`template tags that aren't
478+
thread safe <template_tag_thread_safety>`.
479+
474480
.. _deprecated-features-1.11:
475481

476482
Features deprecated in 1.11

tests/template_backends/test_django.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ def test_autoescape_default(self):
130130
engines['django'].from_string('Hello, {{ name }}').render({'name': 'Bob & Jim'}),
131131
'Hello, Bob &amp; Jim'
132132
)
133+
134+
default_loaders = [
135+
'django.template.loaders.filesystem.Loader',
136+
'django.template.loaders.app_directories.Loader',
137+
]
138+
139+
@override_settings(DEBUG=False)
140+
def test_non_debug_default_template_loaders(self):
141+
engine = DjangoTemplates({'DIRS': [], 'APP_DIRS': True, 'NAME': 'django', 'OPTIONS': {}})
142+
self.assertEqual(engine.engine.loaders, [('django.template.loaders.cached.Loader', self.default_loaders)])
143+
144+
@override_settings(DEBUG=True)
145+
def test_debug_default_template_loaders(self):
146+
engine = DjangoTemplates({'DIRS': [], 'APP_DIRS': True, 'NAME': 'django', 'OPTIONS': {}})
147+
self.assertEqual(engine.engine.loaders, self.default_loaders)

tests/template_loader/tests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
'APP_DIRS': True,
1212
}, {
1313
'BACKEND': 'django.template.backends.django.DjangoTemplates',
14-
'APP_DIRS': True,
1514
'OPTIONS': {
1615
'context_processors': [
1716
'django.template.context_processors.request',
1817
],
18+
'loaders': [
19+
'django.template.loaders.filesystem.Loader',
20+
'django.template.loaders.app_directories.Loader',
21+
]
1922
},
2023
}])
2124
class TemplateLoaderTests(SimpleTestCase):

tests/template_tests/test_loaders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class FileSystemLoaderTests(SimpleTestCase):
279279

280280
@classmethod
281281
def setUpClass(cls):
282-
cls.engine = Engine(dirs=[TEMPLATE_DIR])
282+
cls.engine = Engine(dirs=[TEMPLATE_DIR], loaders=['django.template.loaders.filesystem.Loader'])
283283
super(FileSystemLoaderTests, cls).setUpClass()
284284

285285
@contextmanager

0 commit comments

Comments
 (0)