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
|
|
|
|
2007-07-23 04:45:01 +00:00
|
|
|
import os
|
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
|
2008-08-05 14:16:13 +00:00
|
|
|
from django.core import urlresolvers
|
2015-02-09 18:19:34 +00:00
|
|
|
from django.template import (
|
2015-02-21 19:20:20 +00:00
|
|
|
Context, Template, TemplateSyntaxError, engines, loader,
|
2015-02-09 18:19:34 +00:00
|
|
|
)
|
2015-02-21 19:15:22 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2012-12-08 10:13:52 +00:00
|
|
|
from django.utils._os import upath
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2015-02-24 13:59:05 +00:00
|
|
|
TEMPLATES_DIR = os.path.join(os.path.dirname(upath(__file__)), 'templates')
|
2014-12-17 21:10:57 +00:00
|
|
|
|
|
|
|
|
2015-02-24 13:59:05 +00:00
|
|
|
class TemplateTests(SimpleTestCase):
|
2013-08-30 19:08:40 +00:00
|
|
|
|
2015-02-15 14:42:05 +00:00
|
|
|
@override_settings(DEBUG=True)
|
2013-08-30 19:08:40 +00:00
|
|
|
def test_string_origin(self):
|
2014-11-15 19:58:26 +00:00
|
|
|
template = Template('string template')
|
|
|
|
self.assertEqual(template.origin.source, 'string template')
|
2013-08-30 19:08:40 +00:00
|
|
|
|
2015-02-24 13:59:05 +00:00
|
|
|
|
2014-12-03 20:36:17 +00:00
|
|
|
class TemplateRegressionTests(SimpleTestCase):
|
2013-06-06 12:27:00 +00:00
|
|
|
|
2015-02-15 14:42:05 +00:00
|
|
|
@override_settings(SETTINGS_MODULE=None, DEBUG=True)
|
2009-04-01 22:46:46 +00:00
|
|
|
def test_url_reverse_no_settings_module(self):
|
2009-04-04 19:34:52 +00:00
|
|
|
# Regression test for #9005
|
2009-04-01 22:46:46 +00:00
|
|
|
t = Template('{% url will_not_match %}')
|
|
|
|
c = Context()
|
2011-09-16 01:16:25 +00:00
|
|
|
with self.assertRaises(urlresolvers.NoReverseMatch):
|
|
|
|
t.render(c)
|
|
|
|
|
2014-12-14 22:13:03 +00:00
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'OPTIONS': {'string_if_invalid': '%s is invalid'},
|
|
|
|
}],
|
|
|
|
SETTINGS_MODULE='also_something',
|
|
|
|
)
|
2013-02-23 16:41:30 +00: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 10:02:56 +00:00
|
|
|
self.assertGreater(depth, 5,
|
2013-02-23 16:41:30 +00:00
|
|
|
"The traceback context was lost when reraising the traceback. See #19827")
|
|
|
|
|
2015-02-15 14:42:05 +00:00
|
|
|
@override_settings(DEBUG=True)
|
2011-09-16 01:16:25 +00:00
|
|
|
def test_no_wrapped_exception(self):
|
|
|
|
"""
|
|
|
|
The template system doesn't wrap exceptions, but annotates them.
|
|
|
|
Refs #16770
|
|
|
|
"""
|
|
|
|
c = Context({"coconuts": lambda: 42 / 0})
|
|
|
|
t = Template("{{ coconuts }}")
|
|
|
|
with self.assertRaises(ZeroDivisionError) as cm:
|
|
|
|
t.render(c)
|
|
|
|
|
|
|
|
self.assertEqual(cm.exception.django_template_source[1], (0, 14))
|
2009-04-04 19:34:52 +00:00
|
|
|
|
2010-02-21 23:38:33 +00:00
|
|
|
def test_invalid_block_suggestion(self):
|
|
|
|
# See #7876
|
|
|
|
try:
|
2013-10-19 12:31:38 +00:00
|
|
|
Template("{% if 1 %}lala{% endblock %}{% endif %}")
|
2012-04-28 16:09:37 +00:00
|
|
|
except TemplateSyntaxError as e:
|
2011-12-09 22:13:27 +00:00
|
|
|
self.assertEqual(e.args[0], "Invalid block tag: 'endblock', expected 'elif', 'else' or 'endif'")
|
2010-02-21 23:38:33 +00:00
|
|
|
|
2013-05-30 07:25:58 +00:00
|
|
|
def test_super_errors(self):
|
|
|
|
"""
|
|
|
|
Test behavior of the raise errors into included blocks.
|
|
|
|
See #18169
|
|
|
|
"""
|
|
|
|
t = loader.get_template('included_content.html')
|
|
|
|
with self.assertRaises(urlresolvers.NoReverseMatch):
|
2015-01-08 14:03:43 +00:00
|
|
|
t.render()
|
2013-05-30 07:25:58 +00:00
|
|
|
|
2014-07-21 17:38:34 +00:00
|
|
|
def test_debug_tag_non_ascii(self):
|
|
|
|
"""
|
|
|
|
Test non-ASCII model representation in debug output (#23060).
|
|
|
|
"""
|
|
|
|
Group.objects.create(name="清風")
|
|
|
|
c1 = Context({"objs": Group.objects.all()})
|
|
|
|
t1 = Template('{% debug %}')
|
|
|
|
self.assertIn("清風", t1.render(c1))
|
|
|
|
|
2015-02-14 09:21:06 +00:00
|
|
|
def test_extends_generic_template(self):
|
|
|
|
"""
|
|
|
|
{% extends %} accepts django.template.backends.django.Template (#24338).
|
|
|
|
"""
|
|
|
|
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')
|