2009-03-31 15:23:50 +08:00
|
|
|
import sys
|
2007-07-23 12:45:01 +08:00
|
|
|
|
2021-05-08 04:49:07 +08:00
|
|
|
from django.template import Context, Engine, TemplateDoesNotExist, TemplateSyntaxError
|
2015-03-04 05:48:26 +08:00
|
|
|
from django.template.base import UNKNOWN_SOURCE
|
2015-02-22 03:15:22 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import NoReverseMatch
|
2016-04-03 16:02:20 +08:00
|
|
|
from django.utils import translation
|
2021-05-08 04:49:07 +08:00
|
|
|
from django.utils.html import escape
|
2014-12-18 05:10:57 +08:00
|
|
|
|
|
|
|
|
2021-08-06 16:09:31 +08:00
|
|
|
class TemplateTestMixin:
|
|
|
|
def _engine(self, **kwargs):
|
|
|
|
return Engine(debug=self.debug_engine, **kwargs)
|
2013-08-31 03:08:40 +08:00
|
|
|
|
|
|
|
def test_string_origin(self):
|
2021-08-06 16:09:31 +08:00
|
|
|
template = self._engine().from_string("string template")
|
2015-03-04 05:48:26 +08:00
|
|
|
self.assertEqual(template.origin.name, UNKNOWN_SOURCE)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(template.origin.loader_name)
|
2015-03-04 05:48:26 +08:00
|
|
|
self.assertEqual(template.source, "string template")
|
2013-08-31 03:08:40 +08:00
|
|
|
|
2015-04-12 07:41:45 +08:00
|
|
|
@override_settings(SETTINGS_MODULE=None)
|
2009-04-02 06:46:46 +08:00
|
|
|
def test_url_reverse_no_settings_module(self):
|
2015-04-12 07:41:45 +08:00
|
|
|
"""
|
|
|
|
#9005 -- url tag shouldn't require settings.SETTINGS_MODULE to
|
|
|
|
be set.
|
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
t = self._engine().from_string("{% url will_not_match %}")
|
2009-04-02 06:46:46 +08:00
|
|
|
c = Context()
|
2015-12-30 23:51:16 +08:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
2011-09-16 09:16:25 +08:00
|
|
|
t.render(c)
|
|
|
|
|
2013-02-24 00:41:30 +08:00
|
|
|
def test_url_reverse_view_name(self):
|
2015-04-12 07:41:45 +08:00
|
|
|
"""
|
2023-03-20 15:07:23 +08:00
|
|
|
#19827 -- url tag should keep original stack trace when reraising
|
2015-04-12 07:41:45 +08:00
|
|
|
exception.
|
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
t = self._engine().from_string("{% url will_not_match %}")
|
2013-02-24 00:41:30 +08:00
|
|
|
c = Context()
|
|
|
|
try:
|
|
|
|
t.render(c)
|
2015-12-30 23:51:16 +08:00
|
|
|
except NoReverseMatch:
|
2013-02-24 00:41:30 +08:00
|
|
|
tb = sys.exc_info()[2]
|
|
|
|
depth = 0
|
|
|
|
while tb.tb_next is not None:
|
|
|
|
tb = tb.tb_next
|
|
|
|
depth += 1
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertGreater(
|
|
|
|
depth, 5, "The traceback context was lost when reraising the traceback."
|
|
|
|
)
|
2013-02-24 00:41:30 +08:00
|
|
|
|
2011-09-16 09:16:25 +08:00
|
|
|
def test_no_wrapped_exception(self):
|
|
|
|
"""
|
2015-02-24 05:26:55 +08:00
|
|
|
# 16770 -- The template system doesn't wrap exceptions, but annotates
|
|
|
|
them.
|
2011-09-16 09:16:25 +08:00
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine()
|
2011-09-16 09:16:25 +08:00
|
|
|
c = Context({"coconuts": lambda: 42 / 0})
|
2015-03-06 23:53:25 +08:00
|
|
|
t = engine.from_string("{{ coconuts }}")
|
|
|
|
|
|
|
|
with self.assertRaises(ZeroDivisionError) as e:
|
2011-09-16 09:16:25 +08:00
|
|
|
t.render(c)
|
|
|
|
|
2021-08-06 16:09:31 +08:00
|
|
|
if self.debug_engine:
|
|
|
|
debug = e.exception.template_debug
|
|
|
|
self.assertEqual(debug["start"], 0)
|
|
|
|
self.assertEqual(debug["end"], 14)
|
2009-04-05 03:34:52 +08:00
|
|
|
|
2010-02-22 07:38:33 +08:00
|
|
|
def test_invalid_block_suggestion(self):
|
2015-02-24 05:26:55 +08:00
|
|
|
"""
|
2016-04-03 16:02:20 +08:00
|
|
|
Error messages should include the unexpected block name and be in all
|
|
|
|
English.
|
2015-02-24 05:26:55 +08:00
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine()
|
2015-10-05 01:50:18 +08:00
|
|
|
msg = (
|
|
|
|
"Invalid block tag on line 1: 'endblock', expected 'elif', 'else' "
|
|
|
|
"or 'endif'. Did you forget to register or load this tag?"
|
|
|
|
)
|
2016-04-03 16:02:20 +08:00
|
|
|
with self.settings(USE_I18N=True), translation.override("de"):
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
engine.from_string("{% if 1 %}lala{% endblock %}{% endif %}")
|
2015-02-24 05:26:55 +08:00
|
|
|
|
2015-10-05 01:50:18 +08:00
|
|
|
def test_unknown_block_tag(self):
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine()
|
2015-10-05 01:50:18 +08:00
|
|
|
msg = (
|
|
|
|
"Invalid block tag on line 1: 'foobar'. Did you forget to "
|
|
|
|
"register or load this tag?"
|
2015-02-24 05:26:55 +08:00
|
|
|
)
|
2015-10-05 01:50:18 +08:00
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
engine.from_string("lala{% foobar %}")
|
2010-02-22 07:38:33 +08:00
|
|
|
|
2015-03-06 23:53:25 +08:00
|
|
|
def test_compile_filter_expression_error(self):
|
|
|
|
"""
|
|
|
|
19819 -- Make sure the correct token is highlighted for
|
|
|
|
FilterExpression errors.
|
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine()
|
2015-03-06 23:53:25 +08:00
|
|
|
msg = "Could not parse the remainder: '@bar' from 'foo@bar'"
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg) as e:
|
|
|
|
engine.from_string("{% if 1 %}{{ foo@bar }}{% endif %}")
|
|
|
|
|
2021-08-06 16:09:31 +08:00
|
|
|
if self.debug_engine:
|
|
|
|
debug = e.exception.template_debug
|
|
|
|
self.assertEqual((debug["start"], debug["end"]), (10, 23))
|
|
|
|
self.assertEqual((debug["during"]), "{{ foo@bar }}")
|
2015-03-06 23:53:25 +08:00
|
|
|
|
|
|
|
def test_compile_tag_error(self):
|
|
|
|
"""
|
|
|
|
Errors raised while compiling nodes should include the token
|
|
|
|
information.
|
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine(
|
2015-05-09 04:10:36 +08:00
|
|
|
libraries={"bad_tag": "template_tests.templatetags.bad_tag"},
|
|
|
|
)
|
2015-03-06 23:53:25 +08:00
|
|
|
with self.assertRaises(RuntimeError) as e:
|
|
|
|
engine.from_string("{% load bad_tag %}{% badtag %}")
|
2021-08-06 16:09:31 +08:00
|
|
|
if self.debug_engine:
|
|
|
|
self.assertEqual(e.exception.template_debug["during"], "{% badtag %}")
|
2015-03-06 23:53:25 +08:00
|
|
|
|
2016-12-14 10:35:21 +08:00
|
|
|
def test_compile_tag_error_27584(self):
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine(
|
2016-12-14 10:35:21 +08:00
|
|
|
app_dirs=True,
|
|
|
|
libraries={"tag_27584": "template_tests.templatetags.tag_27584"},
|
|
|
|
)
|
|
|
|
t = engine.get_template("27584_parent.html")
|
|
|
|
with self.assertRaises(TemplateSyntaxError) as e:
|
|
|
|
t.render(Context())
|
2021-08-06 16:09:31 +08:00
|
|
|
if self.debug_engine:
|
|
|
|
self.assertEqual(e.exception.template_debug["during"], "{% badtag %}")
|
2016-12-14 10:35:21 +08:00
|
|
|
|
2017-03-25 20:22:12 +08:00
|
|
|
def test_compile_tag_error_27956(self):
|
|
|
|
"""Errors in a child of {% extends %} are displayed correctly."""
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine(
|
2017-03-25 20:22:12 +08:00
|
|
|
app_dirs=True,
|
|
|
|
libraries={"tag_27584": "template_tests.templatetags.tag_27584"},
|
|
|
|
)
|
|
|
|
t = engine.get_template("27956_child.html")
|
|
|
|
with self.assertRaises(TemplateSyntaxError) as e:
|
|
|
|
t.render(Context())
|
2021-08-06 16:09:31 +08:00
|
|
|
if self.debug_engine:
|
|
|
|
self.assertEqual(e.exception.template_debug["during"], "{% badtag %}")
|
2017-03-25 20:22:12 +08:00
|
|
|
|
2023-09-30 01:02:19 +08:00
|
|
|
def test_compile_tag_extra_data(self):
|
|
|
|
"""Custom tags can pass extra data back to template."""
|
|
|
|
engine = self._engine(
|
|
|
|
app_dirs=True,
|
|
|
|
libraries={"custom": "template_tests.templatetags.custom"},
|
|
|
|
)
|
|
|
|
t = engine.from_string("{% load custom %}{% extra_data %}")
|
|
|
|
self.assertEqual(t.extra_data["extra_data"], "CUSTOM_DATA")
|
|
|
|
|
2021-05-08 04:49:07 +08:00
|
|
|
def test_render_tag_error_in_extended_block(self):
|
|
|
|
"""Errors in extended block are displayed correctly."""
|
2021-08-06 16:09:31 +08:00
|
|
|
e = self._engine(app_dirs=True)
|
2021-05-08 04:49:07 +08:00
|
|
|
template = e.get_template("test_extends_block_error.html")
|
|
|
|
context = Context()
|
|
|
|
with self.assertRaises(TemplateDoesNotExist) as cm:
|
|
|
|
template.render(context)
|
2021-08-06 16:09:31 +08:00
|
|
|
if self.debug_engine:
|
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.template_debug["during"],
|
|
|
|
escape('{% include "missing.html" %}'),
|
|
|
|
)
|
2021-05-08 04:49:07 +08:00
|
|
|
|
2013-05-30 15:25:58 +08:00
|
|
|
def test_super_errors(self):
|
|
|
|
"""
|
2015-02-24 05:26:55 +08:00
|
|
|
#18169 -- NoReverseMatch should not be silence in block.super.
|
2013-05-30 15:25:58 +08:00
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine(app_dirs=True)
|
2015-04-12 07:41:45 +08:00
|
|
|
t = engine.get_template("included_content.html")
|
2015-12-30 23:51:16 +08:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
2015-04-12 07:41:45 +08:00
|
|
|
t.render(Context())
|
2013-05-30 15:25:58 +08:00
|
|
|
|
2015-02-14 17:21:06 +08:00
|
|
|
def test_extends_generic_template(self):
|
|
|
|
"""
|
2015-02-24 05:26:55 +08:00
|
|
|
#24338 -- Allow extending django.template.backends.django.Template
|
|
|
|
objects.
|
2015-02-14 17:21:06 +08:00
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
engine = self._engine()
|
2015-04-12 07:41:45 +08:00
|
|
|
parent = engine.from_string("{% block content %}parent{% endblock %}")
|
|
|
|
child = engine.from_string(
|
2015-02-14 17:21:06 +08:00
|
|
|
"{% extends parent %}{% block content %}child{% endblock %}"
|
|
|
|
)
|
2015-04-12 07:41:45 +08:00
|
|
|
self.assertEqual(child.render(Context({"parent": parent})), "child")
|
2016-01-15 10:36:05 +08:00
|
|
|
|
|
|
|
def test_node_origin(self):
|
|
|
|
"""
|
|
|
|
#25848 -- Set origin on Node so debugging tools can determine which
|
|
|
|
template the node came from even if extending or including templates.
|
|
|
|
"""
|
2021-08-06 16:09:31 +08:00
|
|
|
template = self._engine().from_string("content")
|
2016-01-15 10:36:05 +08:00
|
|
|
for node in template.nodelist:
|
|
|
|
self.assertEqual(node.origin, template.origin)
|
2021-08-06 16:09:31 +08:00
|
|
|
|
2022-05-20 12:01:10 +08:00
|
|
|
def test_render_built_in_type_method(self):
|
|
|
|
"""
|
|
|
|
Templates should not crash when rendering methods for built-in types
|
|
|
|
without required arguments.
|
|
|
|
"""
|
|
|
|
template = self._engine().from_string("{{ description.count }}")
|
|
|
|
self.assertEqual(template.render(Context({"description": "test"})), "")
|
|
|
|
|
2021-08-06 16:09:31 +08:00
|
|
|
|
|
|
|
class TemplateTests(TemplateTestMixin, SimpleTestCase):
|
|
|
|
debug_engine = False
|
|
|
|
|
|
|
|
|
|
|
|
class DebugTemplateTests(TemplateTestMixin, SimpleTestCase):
|
|
|
|
debug_engine = True
|