Looked up the default template engine in the list of all engines.
This commit is contained in:
parent
92a2d049a2
commit
6854998c8f
|
@ -1,6 +1,5 @@
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.utils import lru_cache
|
from django.utils import lru_cache
|
||||||
from django.utils import six
|
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.functional import cached_property
|
||||||
from django.utils.module_loading import import_string
|
from django.utils.module_loading import import_string
|
||||||
|
|
||||||
|
from . import engines
|
||||||
from .base import Context, Lexer, Parser, Template, TemplateDoesNotExist
|
from .base import Context, Lexer, Parser, Template, TemplateDoesNotExist
|
||||||
from .context import _builtin_context_processors
|
from .context import _builtin_context_processors
|
||||||
|
|
||||||
|
@ -45,19 +45,38 @@ class Engine(object):
|
||||||
self.string_if_invalid = string_if_invalid
|
self.string_if_invalid = string_if_invalid
|
||||||
self.file_charset = file_charset
|
self.file_charset = file_charset
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
@lru_cache.lru_cache()
|
@lru_cache.lru_cache()
|
||||||
def get_default(cls):
|
def get_default():
|
||||||
"""Transitional method for refactoring."""
|
"""
|
||||||
return cls(
|
When only one DjangoTemplates backend is configured, returns it.
|
||||||
dirs=settings.TEMPLATE_DIRS,
|
|
||||||
allowed_include_roots=settings.ALLOWED_INCLUDE_ROOTS,
|
Raises ImproperlyConfigured otherwise.
|
||||||
context_processors=settings.TEMPLATE_CONTEXT_PROCESSORS,
|
|
||||||
debug=settings.TEMPLATE_DEBUG,
|
This is required for preserving historical APIs that rely on a
|
||||||
loaders=settings.TEMPLATE_LOADERS,
|
globally available, implicitly configured engine such as:
|
||||||
string_if_invalid=settings.TEMPLATE_STRING_IF_INVALID,
|
|
||||||
file_charset=settings.FILE_CHARSET,
|
>>> 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
|
@cached_property
|
||||||
def template_context_processors(self):
|
def template_context_processors(self):
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
from collections import Counter, OrderedDict
|
from collections import Counter, OrderedDict
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
# import warnings
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.utils import lru_cache
|
from django.utils import lru_cache
|
||||||
from django.utils import six
|
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.functional import cached_property
|
||||||
from django.utils.module_loading import import_string
|
from django.utils.module_loading import import_string
|
||||||
|
|
||||||
|
@ -32,10 +32,12 @@ class EngineHandler(object):
|
||||||
self._templates = settings.TEMPLATES
|
self._templates = settings.TEMPLATES
|
||||||
|
|
||||||
if not self._templates:
|
if not self._templates:
|
||||||
warnings.warn(
|
# TODO: re-enable this warning once the entire test suite has been
|
||||||
"You haven't defined a TEMPLATES setting. You must do so "
|
# updated to rely on TEMPLATES instead of legacy settings.
|
||||||
"before upgrading to Django 2.0. Otherwise Django will be "
|
# warnings.warn(
|
||||||
"unable to load templates.", RemovedInDjango20Warning)
|
# "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 = [
|
self._templates = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
|
|
Loading…
Reference in New Issue