1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #33639 -- Enabled cached template loader in development.

This commit is contained in:
Carlton Gibson 2022-04-12 16:21:02 +02:00
parent f4f2afeb45
commit bf7c51a5f4
4 changed files with 48 additions and 32 deletions

View File

@ -38,8 +38,7 @@ class Engine:
loaders = ["django.template.loaders.filesystem.Loader"] loaders = ["django.template.loaders.filesystem.Loader"]
if app_dirs: if app_dirs:
loaders += ["django.template.loaders.app_directories.Loader"] loaders += ["django.template.loaders.app_directories.Loader"]
if not debug: loaders = [("django.template.loaders.cached.Loader", loaders)]
loaders = [("django.template.loaders.cached.Loader", loaders)]
else: else:
if app_dirs: if app_dirs:
raise ImproperlyConfigured( raise ImproperlyConfigured(

View File

@ -99,9 +99,14 @@ overridden by what's passed by
* ``'django.template.loaders.app_directories.Loader'`` if and only if * ``'django.template.loaders.app_directories.Loader'`` if and only if
``app_dirs`` is ``True``. ``app_dirs`` is ``True``.
If ``debug`` is ``False``, these loaders are wrapped in These loaders are then wrapped in
:class:`django.template.loaders.cached.Loader`. :class:`django.template.loaders.cached.Loader`.
.. versionchanged:: 4.1
In older versions, the cached template loader was only enabled by
default when ``DEBUG`` was ``False``.
See :ref:`template-loaders` for details. See :ref:`template-loaders` for details.
* ``string_if_invalid`` is the output, as a string, that the template * ``string_if_invalid`` is the output, as a string, that the template
@ -905,10 +910,9 @@ loaders that come with Django:
.. class:: cached.Loader .. class:: cached.Loader
By default (when :setting:`DEBUG` is ``True``), the template system reads While the Django template system is quite fast, if it needs to read and
and compiles your templates every time they're rendered. While the Django compile your templates every time they're rendered, the overhead from that
template system is quite fast, the overhead from reading and compiling can add up.
templates can add up.
You configure the cached template loader with a list of other loaders that 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 it should wrap. The wrapped loaders are used to locate unknown templates
@ -917,11 +921,9 @@ loaders that come with Django:
subsequent requests to load the same template. subsequent requests to load the same template.
This loader is automatically enabled if :setting:`OPTIONS['loaders'] This loader is automatically enabled if :setting:`OPTIONS['loaders']
<TEMPLATES-OPTIONS>` isn't specified and :setting:`OPTIONS['debug'] <TEMPLATES-OPTIONS>` isn't specified.
<TEMPLATES-OPTIONS>` is ``False`` (the latter option defaults to the value
of :setting:`DEBUG`).
You can also enable template caching with some custom template loaders You can manually specify template caching with some custom template loaders
using settings like this:: using settings like this::
TEMPLATES = [{ TEMPLATES = [{
@ -947,6 +949,12 @@ loaders that come with Django:
information, see :ref:`template tag thread safety considerations information, see :ref:`template tag thread safety considerations
<template_tag_thread_safety>`. <template_tag_thread_safety>`.
.. versionchanged:: 4.1
The cached template loader was enabled whenever ``OPTIONS['loaders']``
is not specified. Previously it was only enabled when ``DEBUG`` was
``False``.
``django.template.loaders.locmem.Loader`` ``django.template.loaders.locmem.Loader``
.. class:: locmem.Loader .. class:: locmem.Loader

View File

@ -339,12 +339,19 @@ Signals
:data:`~django.db.models.signals.post_delete` signals now dispatch the :data:`~django.db.models.signals.post_delete` signals now dispatch the
``origin`` of the deletion. ``origin`` of the deletion.
.. _templates-4.1:
Templates Templates
~~~~~~~~~ ~~~~~~~~~
* :tfilter:`json_script` template filter now allows wrapping in a ``<script>`` * :tfilter:`json_script` template filter now allows wrapping in a ``<script>``
tag without the HTML ``id`` attribute. tag without the HTML ``id`` attribute.
* The :class:`cached template loader <django.template.loaders.cached.Loader>`
is now enabled in development, when :setting:`DEBUG` is ``True``, and
:setting:`OPTIONS['loaders'] <TEMPLATES-OPTIONS>` isn't specified. You may
specify ``OPTIONS['loaders']`` to override this, if necessary.
Tests Tests
~~~~~ ~~~~~
@ -484,6 +491,10 @@ Miscellaneous
* The undocumented ``InlineAdminFormSet.non_form_errors`` property is replaced * The undocumented ``InlineAdminFormSet.non_form_errors`` property is replaced
by the ``non_form_errors()`` method. This is consistent with ``BaseFormSet``. by the ``non_form_errors()`` method. This is consistent with ``BaseFormSet``.
* As per :ref:`above<templates-4.1>`, the cached template loader is now
enabled in development. You may specify ``OPTIONS['loaders']`` to override
this, if necessary.
.. _deprecated-features-4.1: .. _deprecated-features-4.1:
Features deprecated in 4.1 Features deprecated in 4.1

View File

@ -176,27 +176,25 @@ class DjangoTemplatesTests(TemplateStringsTests):
"Hello, Bob &amp; Jim", "Hello, Bob &amp; Jim",
) )
default_loaders = [ def test_default_template_loaders(self):
"django.template.loaders.filesystem.Loader", """The cached template loader is always enabled by default."""
"django.template.loaders.app_directories.Loader", for debug in (True, False):
] with self.subTest(DEBUG=debug), self.settings(DEBUG=debug):
engine = DjangoTemplates(
@override_settings(DEBUG=False) {"DIRS": [], "APP_DIRS": True, "NAME": "django", "OPTIONS": {}}
def test_non_debug_default_template_loaders(self): )
engine = DjangoTemplates( self.assertEqual(
{"DIRS": [], "APP_DIRS": True, "NAME": "django", "OPTIONS": {}} engine.engine.loaders,
) [
self.assertEqual( (
engine.engine.loaders, "django.template.loaders.cached.Loader",
[("django.template.loaders.cached.Loader", self.default_loaders)], [
) "django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
@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)
def test_dirs_pathlib(self): def test_dirs_pathlib(self):
engine = DjangoTemplates( engine = DjangoTemplates(