mirror of https://github.com/django/django.git
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
|
||||
|
||||
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):
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in New Issue