2009-03-31 15:23:50 +08:00
|
|
|
import sys
|
2007-07-23 12:45:01 +08:00
|
|
|
|
2014-07-22 01:38:34 +08:00
|
|
|
from django.contrib.auth.models import Group
|
2015-04-12 07:41:45 +08:00
|
|
|
from django.template import Context, Engine, 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
|
2014-12-18 05:10:57 +08:00
|
|
|
|
|
|
|
|
2015-02-19 05:51:05 +08:00
|
|
|
class TemplateTests(SimpleTestCase):
|
2013-08-31 03:08:40 +08:00
|
|
|
|
|
|
|
def test_string_origin(self):
|
2015-04-12 07:41:45 +08:00
|
|
|
template = 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.
|
|
|
|
"""
|
|
|
|
t = Engine(debug=True).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
|
|
|
"""
|
|
|
|
#19827 -- url tag should keep original strack trace when reraising
|
|
|
|
exception.
|
|
|
|
"""
|
|
|
|
t = 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
|
|
|
"""
|
2015-03-06 23:53:25 +08:00
|
|
|
engine = Engine(debug=True)
|
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)
|
|
|
|
|
2015-03-06 23:53:25 +08:00
|
|
|
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
|
|
|
"""
|
2015-03-06 23:53:25 +08:00
|
|
|
engine = 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):
|
|
|
|
engine = Engine()
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
engine = Engine(debug=True)
|
|
|
|
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 %}")
|
|
|
|
|
|
|
|
debug = e.exception.template_debug
|
|
|
|
self.assertEqual((debug['start'], debug['end']), (10, 23))
|
|
|
|
self.assertEqual((debug['during']), '{{ foo@bar }}')
|
|
|
|
|
|
|
|
def test_compile_tag_error(self):
|
|
|
|
"""
|
|
|
|
Errors raised while compiling nodes should include the token
|
|
|
|
information.
|
|
|
|
"""
|
2015-05-09 04:10:36 +08:00
|
|
|
engine = Engine(
|
|
|
|
debug=True,
|
|
|
|
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 %}")
|
|
|
|
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
|
|
|
|
2016-12-14 10:35:21 +08:00
|
|
|
def test_compile_tag_error_27584(self):
|
|
|
|
engine = Engine(
|
|
|
|
app_dirs=True,
|
|
|
|
debug=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())
|
|
|
|
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
|
|
|
|
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
|
|
|
"""
|
2015-04-12 07:41:45 +08:00
|
|
|
engine = Engine(app_dirs=True)
|
|
|
|
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
|
|
|
|
2014-07-22 01:38:34 +08:00
|
|
|
def test_debug_tag_non_ascii(self):
|
|
|
|
"""
|
2015-02-24 05:26:55 +08:00
|
|
|
#23060 -- Test non-ASCII model representation in debug output.
|
2014-07-22 01:38:34 +08:00
|
|
|
"""
|
2015-04-14 00:18:57 +08:00
|
|
|
group = Group(name="清風")
|
|
|
|
c1 = Context({"objs": [group]})
|
2015-04-12 07:41:45 +08:00
|
|
|
t1 = Engine().from_string('{% debug %}')
|
2014-07-22 01:38:34 +08:00
|
|
|
self.assertIn("清風", t1.render(c1))
|
|
|
|
|
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
|
|
|
"""
|
2015-04-12 07:41:45 +08:00
|
|
|
engine = Engine()
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
template = Engine().from_string('content')
|
|
|
|
for node in template.nodelist:
|
|
|
|
self.assertEqual(node.origin, template.origin)
|