diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-11-27 21:30:35 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-28 16:23:01 +0100 |
| commit | 6854998c8f405248c304cf14ecc46617e8a9f206 (patch) | |
| tree | 201dd7d135572ef65f524393f4c7931f1b2086ab /django/template/engine.py | |
| parent | 92a2d049a253fb499db88efb6cf6c9209f6f251c (diff) | |
Looked up the default template engine in the list of all engines.
Diffstat (limited to 'django/template/engine.py')
| -rw-r--r-- | django/template/engine.py | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/django/template/engine.py b/django/template/engine.py index 5838877a9f..8a48be46cc 100644 --- a/django/template/engine.py +++ b/django/template/engine.py @@ -1,6 +1,5 @@ import warnings -from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.utils import lru_cache from django.utils import six @@ -8,6 +7,7 @@ from django.utils.deprecation import RemovedInDjango20Warning from django.utils.functional import cached_property from django.utils.module_loading import import_string +from . import engines from .base import Context, Lexer, Parser, Template, TemplateDoesNotExist from .context import _builtin_context_processors @@ -45,19 +45,38 @@ class Engine(object): self.string_if_invalid = string_if_invalid self.file_charset = file_charset - @classmethod + @staticmethod @lru_cache.lru_cache() - def get_default(cls): - """Transitional method for refactoring.""" - return cls( - dirs=settings.TEMPLATE_DIRS, - allowed_include_roots=settings.ALLOWED_INCLUDE_ROOTS, - context_processors=settings.TEMPLATE_CONTEXT_PROCESSORS, - debug=settings.TEMPLATE_DEBUG, - loaders=settings.TEMPLATE_LOADERS, - string_if_invalid=settings.TEMPLATE_STRING_IF_INVALID, - file_charset=settings.FILE_CHARSET, - ) + def get_default(): + """ + When only one DjangoTemplates backend is configured, returns it. + + Raises ImproperlyConfigured otherwise. + + This is required for preserving historical APIs that rely on a + globally available, implicitly configured engine such as: + + >>> from django.template import Context, Template + >>> template = Template("Hello {{ name }}!") + >>> context = Context({'name': "world"}) + >>> template.render(context) + 'Hello world!' + """ + # Since DjangoTemplates is a wrapper around this Engine class, a local + # import is mandatory to avoid an import loop. + from django.template.backends.django import DjangoTemplates + django_engines = [engine for engine in engines.all() + if isinstance(engine, DjangoTemplates)] + if len(django_engines) == 1: + # Unwrap the Engine instance inside DjangoTemplates + return django_engines[0].engine + elif len(django_engines) == 0: + raise ImproperlyConfigured( + "No DjangoTemplates backend is configured.") + else: + raise ImproperlyConfigured( + "Several DjangoTemplates backends are configured. " + "You must select one explicitly.") @cached_property def template_context_processors(self): |
