2019-11-07 17:26:22 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from template_tests.test_response import test_processor_name
|
|
|
|
|
2016-12-29 05:03:20 +08:00
|
|
|
from django.template import Context, EngineHandler, RequestContext
|
2014-12-15 04:22:57 +08:00
|
|
|
from django.template.backends.django import DjangoTemplates
|
2015-05-09 04:10:36 +08:00
|
|
|
from django.template.library import InvalidTemplateLibrary
|
2015-09-05 22:25:43 +08:00
|
|
|
from django.test import RequestFactory, override_settings
|
2015-01-07 03:56:54 +08:00
|
|
|
|
2014-12-15 04:22:57 +08:00
|
|
|
from .test_dummy import TemplateStringsTests
|
|
|
|
|
|
|
|
|
|
|
|
class DjangoTemplatesTests(TemplateStringsTests):
|
|
|
|
engine_class = DjangoTemplates
|
|
|
|
backend_name = "django"
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
2015-01-07 03:56:54 +08:00
|
|
|
|
|
|
|
def test_context_has_priority_over_template_context_processors(self):
|
|
|
|
# See ticket #23789.
|
|
|
|
engine = DjangoTemplates(
|
|
|
|
{
|
|
|
|
"DIRS": [],
|
|
|
|
"APP_DIRS": False,
|
|
|
|
"NAME": "django",
|
|
|
|
"OPTIONS": {
|
|
|
|
"context_processors": [test_processor_name],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-01-07 03:56:54 +08:00
|
|
|
template = engine.from_string("{{ processors }}")
|
2018-11-27 03:01:27 +08:00
|
|
|
request = self.request_factory.get("/")
|
2015-01-07 03:56:54 +08:00
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# Context processors run
|
2015-01-07 03:56:54 +08:00
|
|
|
content = template.render({}, request)
|
|
|
|
self.assertEqual(content, "yes")
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# Context overrides context processors
|
2015-01-07 03:56:54 +08:00
|
|
|
content = template.render({"processors": "no"}, request)
|
|
|
|
self.assertEqual(content, "no")
|
2015-01-08 22:03:43 +08:00
|
|
|
|
2016-12-29 05:03:20 +08:00
|
|
|
def test_render_requires_dict(self):
|
|
|
|
"""django.Template.render() requires a dict."""
|
|
|
|
engine = DjangoTemplates(
|
|
|
|
{
|
|
|
|
"DIRS": [],
|
|
|
|
"APP_DIRS": False,
|
|
|
|
"NAME": "django",
|
|
|
|
"OPTIONS": {},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
template = engine.from_string("")
|
|
|
|
context = Context()
|
2018-11-27 03:01:27 +08:00
|
|
|
request_context = RequestContext(self.request_factory.get("/"), {})
|
2016-12-29 05:03:20 +08:00
|
|
|
msg = "context must be a dict rather than Context."
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
template.render(context)
|
|
|
|
msg = "context must be a dict rather than RequestContext."
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
template.render(request_context)
|
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
@override_settings(INSTALLED_APPS=["template_backends.apps.good"])
|
|
|
|
def test_templatetag_discovery(self):
|
|
|
|
engine = DjangoTemplates(
|
|
|
|
{
|
|
|
|
"DIRS": [],
|
|
|
|
"APP_DIRS": False,
|
|
|
|
"NAME": "django",
|
|
|
|
"OPTIONS": {
|
|
|
|
"libraries": {
|
|
|
|
"alternate": (
|
|
|
|
"template_backends.apps.good.templatetags.good_tags"
|
|
|
|
),
|
|
|
|
"override": (
|
|
|
|
"template_backends.apps.good.templatetags.good_tags"
|
|
|
|
),
|
2022-02-04 03:24:19 +08:00
|
|
|
},
|
2015-05-09 04:10:36 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# libraries are discovered from installed applications
|
|
|
|
self.assertEqual(
|
|
|
|
engine.engine.libraries["good_tags"],
|
|
|
|
"template_backends.apps.good.templatetags.good_tags",
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
engine.engine.libraries["subpackage.tags"],
|
|
|
|
"template_backends.apps.good.templatetags.subpackage.tags",
|
|
|
|
)
|
|
|
|
# libraries are discovered from django.templatetags
|
|
|
|
self.assertEqual(
|
|
|
|
engine.engine.libraries["static"],
|
|
|
|
"django.templatetags.static",
|
|
|
|
)
|
|
|
|
# libraries passed in OPTIONS are registered
|
|
|
|
self.assertEqual(
|
|
|
|
engine.engine.libraries["alternate"],
|
|
|
|
"template_backends.apps.good.templatetags.good_tags",
|
|
|
|
)
|
|
|
|
# libraries passed in OPTIONS take precedence over discovered ones
|
|
|
|
self.assertEqual(
|
|
|
|
engine.engine.libraries["override"],
|
|
|
|
"template_backends.apps.good.templatetags.good_tags",
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(INSTALLED_APPS=["template_backends.apps.importerror"])
|
|
|
|
def test_templatetag_discovery_import_error(self):
|
|
|
|
"""
|
|
|
|
Import errors in tag modules should be reraised with a helpful message.
|
|
|
|
"""
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
InvalidTemplateLibrary,
|
|
|
|
"ImportError raised when trying to load "
|
|
|
|
"'template_backends.apps.importerror.templatetags.broken_tags'",
|
2020-12-27 23:23:16 +08:00
|
|
|
) as cm:
|
2015-05-09 04:10:36 +08:00
|
|
|
DjangoTemplates(
|
|
|
|
{
|
|
|
|
"DIRS": [],
|
|
|
|
"APP_DIRS": False,
|
|
|
|
"NAME": "django",
|
|
|
|
"OPTIONS": {},
|
|
|
|
}
|
|
|
|
)
|
2020-12-27 23:23:16 +08:00
|
|
|
self.assertIsInstance(cm.exception.__cause__, ImportError)
|
2015-05-09 04:10:36 +08:00
|
|
|
|
|
|
|
def test_builtins_discovery(self):
|
|
|
|
engine = DjangoTemplates(
|
|
|
|
{
|
|
|
|
"DIRS": [],
|
|
|
|
"APP_DIRS": False,
|
|
|
|
"NAME": "django",
|
|
|
|
"OPTIONS": {
|
|
|
|
"builtins": ["template_backends.apps.good.templatetags.good_tags"],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
engine.engine.builtins,
|
|
|
|
[
|
|
|
|
"django.template.defaulttags",
|
|
|
|
"django.template.defaultfilters",
|
|
|
|
"django.template.loader_tags",
|
|
|
|
"template_backends.apps.good.templatetags.good_tags",
|
|
|
|
],
|
|
|
|
)
|
2015-11-08 17:06:07 +08:00
|
|
|
|
|
|
|
def test_autoescape_off(self):
|
|
|
|
templates = [
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
|
|
|
"OPTIONS": {"autoescape": False},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
engines = EngineHandler(templates=templates)
|
|
|
|
self.assertEqual(
|
|
|
|
engines["django"]
|
|
|
|
.from_string("Hello, {{ name }}")
|
|
|
|
.render({"name": "Bob & Jim"}),
|
|
|
|
"Hello, Bob & Jim",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_autoescape_default(self):
|
|
|
|
templates = [
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
engines = EngineHandler(templates=templates)
|
|
|
|
self.assertEqual(
|
|
|
|
engines["django"]
|
|
|
|
.from_string("Hello, {{ name }}")
|
|
|
|
.render({"name": "Bob & Jim"}),
|
|
|
|
"Hello, Bob & Jim",
|
|
|
|
)
|
2016-09-03 21:06:33 +08:00
|
|
|
|
2022-04-12 22:21:02 +08:00
|
|
|
def test_default_template_loaders(self):
|
|
|
|
"""The cached template loader is always enabled by default."""
|
|
|
|
for debug in (True, False):
|
|
|
|
with self.subTest(DEBUG=debug), self.settings(DEBUG=debug):
|
|
|
|
engine = DjangoTemplates(
|
|
|
|
{"DIRS": [], "APP_DIRS": True, "NAME": "django", "OPTIONS": {}}
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
engine.engine.loaders,
|
|
|
|
[
|
|
|
|
(
|
|
|
|
"django.template.loaders.cached.Loader",
|
|
|
|
[
|
|
|
|
"django.template.loaders.filesystem.Loader",
|
|
|
|
"django.template.loaders.app_directories.Loader",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2019-11-07 17:26:22 +08:00
|
|
|
|
|
|
|
def test_dirs_pathlib(self):
|
|
|
|
engine = DjangoTemplates(
|
|
|
|
{
|
|
|
|
"DIRS": [Path(__file__).parent / "templates" / "template_backends"],
|
|
|
|
"APP_DIRS": False,
|
|
|
|
"NAME": "django",
|
|
|
|
"OPTIONS": {},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
template = engine.get_template("hello.html")
|
|
|
|
self.assertEqual(template.render({"name": "Joe"}), "Hello Joe!\n")
|