2006-12-05 19:48:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-09-16 01:16:25 +00:00
|
|
|
|
2009-03-31 07:23:50 +00:00
|
|
|
import sys
|
2007-07-23 04:45:01 +00:00
|
|
|
|
2014-07-21 17:38:34 +00:00
|
|
|
from django.contrib.auth.models import Group
|
2015-04-11 23:41:45 +00:00
|
|
|
from django.template import Context, Engine, TemplateSyntaxError
|
2015-03-03 21:48:26 +00:00
|
|
|
from django.template.base import UNKNOWN_SOURCE
|
2015-02-21 19:15:22 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-12-30 15:51:16 +00:00
|
|
|
from django.urls import NoReverseMatch
|
2016-04-03 08:02:20 +00:00
|
|
|
from django.utils import translation
|
2014-12-17 21:10:57 +00:00
|
|
|
|
|
|
|
|
2015-02-18 21:51:05 +00:00
|
|
|
class TemplateTests(SimpleTestCase):
|
2013-08-30 19:08:40 +00:00
|
|
|
|
|
|
|
def test_string_origin(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
template = Engine().from_string('string template')
|
2015-03-03 21:48:26 +00:00
|
|
|
self.assertEqual(template.origin.name, UNKNOWN_SOURCE)
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(template.origin.loader_name)
|
2015-03-03 21:48:26 +00:00
|
|
|
self.assertEqual(template.source, 'string template')
|
2013-08-30 19:08:40 +00:00
|
|
|
|
2015-04-11 23:41:45 +00:00
|
|
|
@override_settings(SETTINGS_MODULE=None)
|
2009-04-01 22:46:46 +00:00
|
|
|
def test_url_reverse_no_settings_module(self):
|
2015-04-11 23:41:45 +00: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-01 22:46:46 +00:00
|
|
|
c = Context()
|
2015-12-30 15:51:16 +00:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
2011-09-16 01:16:25 +00:00
|
|
|
t.render(c)
|
|
|
|
|
2013-02-23 16:41:30 +00:00
|
|
|
def test_url_reverse_view_name(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
"""
|
|
|
|
#19827 -- url tag should keep original strack trace when reraising
|
|
|
|
exception.
|
|
|
|
"""
|
|
|
|
t = Engine().from_string('{% url will_not_match %}')
|
2013-02-23 16:41:30 +00:00
|
|
|
c = Context()
|
|
|
|
try:
|
|
|
|
t.render(c)
|
2015-12-30 15:51:16 +00:00
|
|
|
except NoReverseMatch:
|
2013-02-23 16:41:30 +00:00
|
|
|
tb = sys.exc_info()[2]
|
|
|
|
depth = 0
|
|
|
|
while tb.tb_next is not None:
|
|
|
|
tb = tb.tb_next
|
|
|
|
depth += 1
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertGreater(depth, 5, "The traceback context was lost when reraising the traceback.")
|
2013-02-23 16:41:30 +00:00
|
|
|
|
2011-09-16 01:16:25 +00:00
|
|
|
def test_no_wrapped_exception(self):
|
|
|
|
"""
|
2015-02-23 21:26:55 +00:00
|
|
|
# 16770 -- The template system doesn't wrap exceptions, but annotates
|
|
|
|
them.
|
2011-09-16 01:16:25 +00:00
|
|
|
"""
|
2015-03-06 15:53:25 +00:00
|
|
|
engine = Engine(debug=True)
|
2011-09-16 01:16:25 +00:00
|
|
|
c = Context({"coconuts": lambda: 42 / 0})
|
2015-03-06 15:53:25 +00:00
|
|
|
t = engine.from_string("{{ coconuts }}")
|
|
|
|
|
|
|
|
with self.assertRaises(ZeroDivisionError) as e:
|
2011-09-16 01:16:25 +00:00
|
|
|
t.render(c)
|
|
|
|
|
2015-03-06 15:53:25 +00:00
|
|
|
debug = e.exception.template_debug
|
|
|
|
self.assertEqual(debug['start'], 0)
|
|
|
|
self.assertEqual(debug['end'], 14)
|
2009-04-04 19:34:52 +00:00
|
|
|
|
2010-02-21 23:38:33 +00:00
|
|
|
def test_invalid_block_suggestion(self):
|
2015-02-23 21:26:55 +00:00
|
|
|
"""
|
2016-04-03 08:02:20 +00:00
|
|
|
Error messages should include the unexpected block name and be in all
|
|
|
|
English.
|
2015-02-23 21:26:55 +00:00
|
|
|
"""
|
2015-03-06 15:53:25 +00:00
|
|
|
engine = Engine()
|
2015-10-04 17:50:18 +00: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 08:02:20 +00: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-23 21:26:55 +00:00
|
|
|
|
2015-10-04 17:50:18 +00: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-23 21:26:55 +00:00
|
|
|
)
|
2015-10-04 17:50:18 +00:00
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
engine.from_string("lala{% foobar %}")
|
2010-02-21 23:38:33 +00:00
|
|
|
|
2015-03-06 15:53:25 +00: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-08 20:10:36 +00:00
|
|
|
engine = Engine(
|
|
|
|
debug=True,
|
|
|
|
libraries={'bad_tag': 'template_tests.templatetags.bad_tag'},
|
|
|
|
)
|
2015-03-06 15:53:25 +00:00
|
|
|
with self.assertRaises(RuntimeError) as e:
|
|
|
|
engine.from_string("{% load bad_tag %}{% badtag %}")
|
|
|
|
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
|
|
|
|
|
2013-05-30 07:25:58 +00:00
|
|
|
def test_super_errors(self):
|
|
|
|
"""
|
2015-02-23 21:26:55 +00:00
|
|
|
#18169 -- NoReverseMatch should not be silence in block.super.
|
2013-05-30 07:25:58 +00:00
|
|
|
"""
|
2015-04-11 23:41:45 +00:00
|
|
|
engine = Engine(app_dirs=True)
|
|
|
|
t = engine.get_template('included_content.html')
|
2015-12-30 15:51:16 +00:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
2015-04-11 23:41:45 +00:00
|
|
|
t.render(Context())
|
2013-05-30 07:25:58 +00:00
|
|
|
|
2014-07-21 17:38:34 +00:00
|
|
|
def test_debug_tag_non_ascii(self):
|
|
|
|
"""
|
2015-02-23 21:26:55 +00:00
|
|
|
#23060 -- Test non-ASCII model representation in debug output.
|
2014-07-21 17:38:34 +00:00
|
|
|
"""
|
2015-04-13 16:18:57 +00:00
|
|
|
group = Group(name="清風")
|
|
|
|
c1 = Context({"objs": [group]})
|
2015-04-11 23:41:45 +00:00
|
|
|
t1 = Engine().from_string('{% debug %}')
|
2014-07-21 17:38:34 +00:00
|
|
|
self.assertIn("清風", t1.render(c1))
|
|
|
|
|
2015-02-14 09:21:06 +00:00
|
|
|
def test_extends_generic_template(self):
|
|
|
|
"""
|
2015-02-23 21:26:55 +00:00
|
|
|
#24338 -- Allow extending django.template.backends.django.Template
|
|
|
|
objects.
|
2015-02-14 09:21:06 +00:00
|
|
|
"""
|
2015-04-11 23:41:45 +00:00
|
|
|
engine = Engine()
|
|
|
|
parent = engine.from_string('{% block content %}parent{% endblock %}')
|
|
|
|
child = engine.from_string(
|
2015-02-14 09:21:06 +00:00
|
|
|
'{% extends parent %}{% block content %}child{% endblock %}')
|
2015-04-11 23:41:45 +00:00
|
|
|
self.assertEqual(child.render(Context({'parent': parent})), 'child')
|
2016-01-15 02:36:05 +00: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)
|