2015-02-19 05:51:05 +08:00
|
|
|
import os
|
|
|
|
|
2017-03-31 22:54:00 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-02-19 05:51:05 +08:00
|
|
|
from django.template import Context
|
|
|
|
from django.template.engine import Engine
|
2017-03-31 22:54:00 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-02-19 05:51:05 +08:00
|
|
|
|
|
|
|
from .utils import ROOT, TEMPLATE_DIR
|
|
|
|
|
|
|
|
OTHER_DIR = os.path.join(ROOT, 'other_templates')
|
|
|
|
|
|
|
|
|
2021-05-28 08:45:40 +08:00
|
|
|
class EngineTest(SimpleTestCase):
|
|
|
|
def test_repr_empty(self):
|
|
|
|
engine = Engine()
|
|
|
|
self.assertEqual(
|
|
|
|
repr(engine),
|
|
|
|
"<Engine: app_dirs=False debug=False loaders=[("
|
|
|
|
"'django.template.loaders.cached.Loader', "
|
|
|
|
"['django.template.loaders.filesystem.Loader'])] "
|
|
|
|
"string_if_invalid='' file_charset='utf-8' builtins=["
|
|
|
|
"'django.template.defaulttags', 'django.template.defaultfilters', "
|
|
|
|
"'django.template.loader_tags'] autoescape=True>"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
engine = Engine(
|
|
|
|
dirs=[TEMPLATE_DIR],
|
|
|
|
context_processors=['django.template.context_processors.debug'],
|
|
|
|
debug=True,
|
|
|
|
loaders=['django.template.loaders.filesystem.Loader'],
|
|
|
|
string_if_invalid='x',
|
|
|
|
file_charset='utf-16',
|
|
|
|
libraries={'custom': 'template_tests.templatetags.custom'},
|
|
|
|
autoescape=False,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(engine),
|
|
|
|
f"<Engine: dirs=[{TEMPLATE_DIR!r}] app_dirs=False "
|
|
|
|
"context_processors=['django.template.context_processors.debug'] "
|
|
|
|
"debug=True loaders=['django.template.loaders.filesystem.Loader'] "
|
|
|
|
"string_if_invalid='x' file_charset='utf-16' "
|
|
|
|
"libraries={'custom': 'template_tests.templatetags.custom'} "
|
|
|
|
"builtins=['django.template.defaulttags', "
|
|
|
|
"'django.template.defaultfilters', 'django.template.loader_tags'] "
|
|
|
|
"autoescape=False>"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-09-04 10:01:30 +08:00
|
|
|
class RenderToStringTest(SimpleTestCase):
|
2015-02-19 05:51:05 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2015-09-04 10:01:30 +08:00
|
|
|
self.engine = Engine(dirs=[TEMPLATE_DIR])
|
2015-02-19 05:51:05 +08:00
|
|
|
|
|
|
|
def test_basic_context(self):
|
|
|
|
self.assertEqual(
|
|
|
|
self.engine.render_to_string('test_context.html', {'obj': 'test'}),
|
|
|
|
'obj:test\n',
|
|
|
|
)
|
|
|
|
|
2019-03-24 04:27:45 +08:00
|
|
|
def test_autoescape_off(self):
|
|
|
|
engine = Engine(dirs=[TEMPLATE_DIR], autoescape=False)
|
|
|
|
self.assertEqual(
|
|
|
|
engine.render_to_string('test_context.html', {'obj': '<script>'}),
|
|
|
|
'obj:<script>\n',
|
|
|
|
)
|
|
|
|
|
2015-02-19 05:51:05 +08:00
|
|
|
|
2017-03-31 22:54:00 +08:00
|
|
|
class GetDefaultTests(SimpleTestCase):
|
|
|
|
|
|
|
|
@override_settings(TEMPLATES=[])
|
|
|
|
def test_no_engines_configured(self):
|
|
|
|
msg = 'No DjangoTemplates backend is configured.'
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
Engine.get_default()
|
|
|
|
|
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'NAME': 'default',
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'OPTIONS': {'file_charset': 'abc'},
|
|
|
|
}])
|
|
|
|
def test_single_engine_configured(self):
|
|
|
|
self.assertEqual(Engine.get_default().file_charset, 'abc')
|
|
|
|
|
|
|
|
@override_settings(TEMPLATES=[{
|
|
|
|
'NAME': 'default',
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
2017-03-11 04:46:37 +08:00
|
|
|
'OPTIONS': {'file_charset': 'abc'},
|
2017-03-31 22:54:00 +08:00
|
|
|
}, {
|
|
|
|
'NAME': 'other',
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
2017-03-11 04:46:37 +08:00
|
|
|
'OPTIONS': {'file_charset': 'def'},
|
2017-03-31 22:54:00 +08:00
|
|
|
}])
|
|
|
|
def test_multiple_engines_configured(self):
|
2017-03-11 04:46:37 +08:00
|
|
|
self.assertEqual(Engine.get_default().file_charset, 'abc')
|
2017-03-31 22:54:00 +08:00
|
|
|
|
|
|
|
|
2015-02-19 05:51:05 +08:00
|
|
|
class LoaderTests(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_origin(self):
|
|
|
|
engine = Engine(dirs=[TEMPLATE_DIR], debug=True)
|
|
|
|
template = engine.get_template('index.html')
|
2015-03-04 05:48:26 +08:00
|
|
|
self.assertEqual(template.origin.template_name, 'index.html')
|
2015-02-19 05:51:05 +08:00
|
|
|
|
|
|
|
def test_loader_priority(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
#21460 -- The order of template loader works.
|
2015-02-19 05:51:05 +08:00
|
|
|
"""
|
|
|
|
loaders = [
|
|
|
|
'django.template.loaders.filesystem.Loader',
|
|
|
|
'django.template.loaders.app_directories.Loader',
|
|
|
|
]
|
|
|
|
engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders)
|
|
|
|
template = engine.get_template('priority/foo.html')
|
|
|
|
self.assertEqual(template.render(Context()), 'priority\n')
|
|
|
|
|
|
|
|
def test_cached_loader_priority(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The order of template loader works. Refs #21460.
|
2015-02-19 05:51:05 +08:00
|
|
|
"""
|
|
|
|
loaders = [
|
|
|
|
('django.template.loaders.cached.Loader', [
|
|
|
|
'django.template.loaders.filesystem.Loader',
|
|
|
|
'django.template.loaders.app_directories.Loader',
|
|
|
|
]),
|
|
|
|
]
|
|
|
|
engine = Engine(dirs=[OTHER_DIR, TEMPLATE_DIR], loaders=loaders)
|
|
|
|
|
|
|
|
template = engine.get_template('priority/foo.html')
|
|
|
|
self.assertEqual(template.render(Context()), 'priority\n')
|
|
|
|
|
|
|
|
template = engine.get_template('priority/foo.html')
|
|
|
|
self.assertEqual(template.render(Context()), 'priority\n')
|