summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2014-11-27 21:30:35 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2014-12-28 16:23:01 +0100
commit6854998c8f405248c304cf14ecc46617e8a9f206 (patch)
tree201dd7d135572ef65f524393f4c7931f1b2086ab
parent92a2d049a253fb499db88efb6cf6c9209f6f251c (diff)
Looked up the default template engine in the list of all engines.
-rw-r--r--django/template/engine.py45
-rw-r--r--django/template/utils.py14
2 files changed, 40 insertions, 19 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):
diff --git a/django/template/utils.py b/django/template/utils.py
index 3303c1ccb7..140d591e53 100644
--- a/django/template/utils.py
+++ b/django/template/utils.py
@@ -1,14 +1,14 @@
from collections import Counter, OrderedDict
import os
import sys
-import warnings
+# import warnings
from django.apps import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils import lru_cache
from django.utils import six
-from django.utils.deprecation import RemovedInDjango20Warning
+# from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.functional import cached_property
from django.utils.module_loading import import_string
@@ -32,10 +32,12 @@ class EngineHandler(object):
self._templates = settings.TEMPLATES
if not self._templates:
- warnings.warn(
- "You haven't defined a TEMPLATES setting. You must do so "
- "before upgrading to Django 2.0. Otherwise Django will be "
- "unable to load templates.", RemovedInDjango20Warning)
+ # TODO: re-enable this warning once the entire test suite has been
+ # updated to rely on TEMPLATES instead of legacy settings.
+ # warnings.warn(
+ # "You haven't defined a TEMPLATES setting. You must do so "
+ # "before upgrading to Django 2.0. Otherwise Django will be "
+ # "unable to load templates.", RemovedInDjango20Warning)
self._templates = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',