2006-12-06 03:48:46 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-09-16 09:16:25 +08:00
|
|
|
|
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
|
2008-08-05 22:16:13 +08:00
|
|
|
from django.core import urlresolvers
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.template import (
|
2015-03-06 23:53:25 +08:00
|
|
|
Context, Engine, Template, TemplateSyntaxError, engines, loader,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2015-02-22 03:15:22 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
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
|
|
|
|
2015-02-15 22:42:05 +08:00
|
|
|
@override_settings(DEBUG=True)
|
2013-08-31 03:08:40 +08:00
|
|
|
def test_string_origin(self):
|
2014-11-16 03:58:26 +08:00
|
|
|
template = Template('string template')
|
|
|
|
self.assertEqual(template.origin.source, 'string template')
|
2013-08-31 03:08:40 +08:00
|
|
|
|
2015-02-15 22:42:05 +08:00
|
|
|
@override_settings(SETTINGS_MODULE=None, DEBUG=True)
|
2009-04-02 06:46:46 +08:00
|
|
|
def test_url_reverse_no_settings_module(self):
|
2009-04-05 03:34:52 +08:00
|
|
|
# Regression test for #9005
|
2009-04-02 06:46:46 +08:00
|
|
|
t = Template('{% url will_not_match %}')
|
|
|
|
c = Context()
|
2011-09-16 09:16:25 +08:00
|
|
|
with self.assertRaises(urlresolvers.NoReverseMatch):
|
|
|
|
t.render(c)
|
|
|
|
|
2014-12-15 06:13:03 +08:00
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'OPTIONS': {'string_if_invalid': '%s is invalid'},
|
|
|
|
}],
|
|
|
|
SETTINGS_MODULE='also_something',
|
|
|
|
)
|
2013-02-24 00:41:30 +08:00
|
|
|
def test_url_reverse_view_name(self):
|
|
|
|
# Regression test for #19827
|
|
|
|
t = Template('{% url will_not_match %}')
|
|
|
|
c = Context()
|
|
|
|
try:
|
|
|
|
t.render(c)
|
|
|
|
except urlresolvers.NoReverseMatch:
|
|
|
|
tb = sys.exc_info()[2]
|
|
|
|
depth = 0
|
|
|
|
while tb.tb_next is not None:
|
|
|
|
tb = tb.tb_next
|
|
|
|
depth += 1
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertGreater(depth, 5,
|
2013-02-24 00:41:30 +08:00
|
|
|
"The traceback context was lost when reraising the traceback. See #19827")
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
#7876 -- Error messages should include the unexpected block name.
|
|
|
|
"""
|
2015-03-06 23:53:25 +08:00
|
|
|
engine = Engine()
|
|
|
|
|
2015-02-24 05:26:55 +08:00
|
|
|
with self.assertRaises(TemplateSyntaxError) as e:
|
2015-03-06 23:53:25 +08:00
|
|
|
engine.from_string("{% if 1 %}lala{% endblock %}{% endif %}")
|
2015-02-24 05:26:55 +08:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
e.exception.args[0],
|
|
|
|
"Invalid block tag: 'endblock', expected 'elif', 'else' or 'endif'",
|
|
|
|
)
|
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.
|
|
|
|
"""
|
|
|
|
engine = Engine(debug=True)
|
|
|
|
with self.assertRaises(RuntimeError) as e:
|
|
|
|
engine.from_string("{% load bad_tag %}{% badtag %}")
|
|
|
|
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
|
|
|
"""
|
|
|
|
t = loader.get_template('included_content.html')
|
|
|
|
with self.assertRaises(urlresolvers.NoReverseMatch):
|
2015-01-08 22:03:43 +08:00
|
|
|
t.render()
|
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
|
|
|
"""
|
|
|
|
Group.objects.create(name="清風")
|
|
|
|
c1 = Context({"objs": Group.objects.all()})
|
|
|
|
t1 = Template('{% debug %}')
|
|
|
|
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
|
|
|
"""
|
|
|
|
parent = engines['django'].from_string(
|
|
|
|
'{% block content %}parent{% endblock %}')
|
|
|
|
child = engines['django'].from_string(
|
|
|
|
'{% extends parent %}{% block content %}child{% endblock %}')
|
|
|
|
self.assertEqual(child.render({'parent': parent}), 'child')
|