Used get_template_loaders in the cached loader.

This ensures that enabling the cached loader doesn't change behavior.

(Before this commit, it did when the list contained unusable loaders.)
This commit is contained in:
Aymeric Augustin 2014-11-15 21:17:43 +01:00
parent 9eeb788cfb
commit e87bee6f50
2 changed files with 10 additions and 17 deletions

View File

@ -6,10 +6,10 @@ to load templates from them in order, caching the result.
import hashlib import hashlib
from django.template.base import TemplateDoesNotExist from django.template.base import TemplateDoesNotExist
from django.template.loader import get_template_from_string, make_origin from django.template.loader import get_template_from_string, make_origin
from django.template.loaders.utils import find_template_loader
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from .base import Loader as BaseLoader from .base import Loader as BaseLoader
from .utils import _get_template_loaders
class Loader(BaseLoader): class Loader(BaseLoader):
@ -18,20 +18,9 @@ class Loader(BaseLoader):
def __init__(self, loaders): def __init__(self, loaders):
self.template_cache = {} self.template_cache = {}
self.find_template_cache = {} self.find_template_cache = {}
self._loaders = loaders # Use the private, non-caching version of get_template_loaders
self._cached_loaders = [] # in case loaders isn't hashable.
self.loaders = _get_template_loaders(loaders)
@property
def loaders(self):
# Resolve loaders on demand to avoid circular imports
if not self._cached_loaders:
# Set self._cached_loaders atomically. Otherwise, another thread
# could see an incomplete list. See #17303.
cached_loaders = []
for loader in self._loaders:
cached_loaders.append(find_template_loader(loader))
self._cached_loaders = cached_loaders
return self._cached_loaders
def cache_key(self, template_name, template_dirs): def cache_key(self, template_name, template_dirs):
if template_dirs: if template_dirs:

View File

@ -10,9 +10,13 @@ from django.utils.module_loading import import_string
@lru_cache.lru_cache() @lru_cache.lru_cache()
def get_template_loaders(): def get_template_loaders():
return _get_template_loaders(settings.TEMPLATE_LOADERS)
def _get_template_loaders(template_loaders=None):
loaders = [] loaders = []
for loader_name in settings.TEMPLATE_LOADERS: for template_loader in template_loaders:
loader = find_template_loader(loader_name) loader = find_template_loader(template_loader)
if loader is not None: if loader is not None:
loaders.append(loader) loaders.append(loader)
# Immutable return value because it will be cached and shared by callers. # Immutable return value because it will be cached and shared by callers.