Skip to content

Commit

Permalink
Fixed #25788 -- Enabled the cached template loader if debug is False.
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Sep 3, 2016
1 parent 2ced2f7 commit 277fe2e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
2 changes: 2 additions & 0 deletions django/template/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, dirs=None, app_dirs=False, context_processors=None,
loaders = ['django.template.loaders.filesystem.Loader']
if app_dirs:
loaders += ['django.template.loaders.app_directories.Loader']
if not debug:
loaders = [('django.template.loaders.cached.Loader', loaders)]
else:
if app_dirs:
raise ImproperlyConfigured(
Expand Down
38 changes: 27 additions & 11 deletions docs/ref/templates/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,16 @@ what's passed by :class:`~django.template.backends.django.DjangoTemplates`.
* ``'django.template.loaders.app_directories.Loader'`` if and only if
``app_dirs`` is ``True``.

If ``debug`` is ``False``, these loaders are wrapped in
:class:`django.template.loaders.cached.Loader`.

See :ref:`template-loaders` for details.

.. versionchanged:: 1.11

Enabling of the cached template loader when ``debug`` is ``False``
was added.

* ``string_if_invalid`` is the output, as a string, that the template
system should use for invalid (e.g. misspelled) variables.

Expand Down Expand Up @@ -899,18 +907,22 @@ loaders that come with Django:

.. class:: cached.Loader

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

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

For example, to enable template caching with the ``filesystem`` and
``app_directories`` template loaders you might use the following settings::
This loader is automatically enabled if :setting:`DEBUG` is ``False`` and
:setting:`OPTIONS['loaders'] <TEMPLATES-OPTIONS>` isn't specified.

You can also enable template caching with some custom template loaders
using settings like this::

TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
Expand All @@ -920,6 +932,7 @@ loaders that come with Django:
('django.template.loaders.cached.Loader', [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'path.to.custom.Loader',
]),
],
},
Expand All @@ -934,7 +947,10 @@ loaders that come with Django:
information, see :ref:`template tag thread safety considerations
<template_tag_thread_safety>`.

This loader is disabled by default.
.. versionchanged:: 1.11

The automatic enabling of the cached template loader when ``debug`` is
``False`` was added.

``django.template.loaders.locmem.Loader``

Expand Down
6 changes: 6 additions & 0 deletions docs/releases/1.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ Miscellaneous
:class:`~django.core.serializers.json.DjangoJSONEncoder` (renamed in Django
1.0) is removed.

* The :class:`cached template loader <django.template.loaders.cached.Loader>`
is now enabled if :setting:`DEBUG` is ``False`` and
:setting:`OPTIONS['loaders'] <TEMPLATES-OPTIONS>` isn't specified. This could
be backwards-incompatible if you have some :ref:`template tags that aren't
thread safe <template_tag_thread_safety>`.

.. _deprecated-features-1.11:

Features deprecated in 1.11
Expand Down
15 changes: 15 additions & 0 deletions tests/template_backends/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,18 @@ def test_autoescape_default(self):
engines['django'].from_string('Hello, {{ name }}').render({'name': 'Bob & Jim'}),
'Hello, Bob &amp; Jim'
)

default_loaders = [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]

@override_settings(DEBUG=False)
def test_non_debug_default_template_loaders(self):
engine = DjangoTemplates({'DIRS': [], 'APP_DIRS': True, 'NAME': 'django', 'OPTIONS': {}})
self.assertEqual(engine.engine.loaders, [('django.template.loaders.cached.Loader', self.default_loaders)])

@override_settings(DEBUG=True)
def test_debug_default_template_loaders(self):
engine = DjangoTemplates({'DIRS': [], 'APP_DIRS': True, 'NAME': 'django', 'OPTIONS': {}})
self.assertEqual(engine.engine.loaders, self.default_loaders)
5 changes: 4 additions & 1 deletion tests/template_loader/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
'APP_DIRS': True,
}, {
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]
},
}])
class TemplateLoaderTests(SimpleTestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/template_tests/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class FileSystemLoaderTests(SimpleTestCase):

@classmethod
def setUpClass(cls):
cls.engine = Engine(dirs=[TEMPLATE_DIR])
cls.engine = Engine(dirs=[TEMPLATE_DIR], loaders=['django.template.loaders.filesystem.Loader'])
super(FileSystemLoaderTests, cls).setUpClass()

@contextmanager
Expand Down

0 comments on commit 277fe2e

Please sign in to comment.