test_include.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import warnings
  2. from django.template import (
  3. Context, Engine, TemplateDoesNotExist, TemplateSyntaxError, loader,
  4. )
  5. from django.test import SimpleTestCase, ignore_warnings
  6. from django.utils.deprecation import RemovedInDjango21Warning
  7. from ..utils import setup
  8. from .test_basic import basic_templates
  9. include_fail_templates = {
  10. 'include-fail1': '{% load bad_tag %}{% badtag %}',
  11. 'include-fail2': '{% load broken_tag %}',
  12. }
  13. class IncludeTagTests(SimpleTestCase):
  14. libraries = {'bad_tag': 'template_tests.templatetags.bad_tag'}
  15. @setup({'include01': '{% include "basic-syntax01" %}'}, basic_templates)
  16. def test_include01(self):
  17. output = self.engine.render_to_string('include01')
  18. self.assertEqual(output, 'something cool')
  19. @setup({'include02': '{% include "basic-syntax02" %}'}, basic_templates)
  20. def test_include02(self):
  21. output = self.engine.render_to_string('include02', {'headline': 'Included'})
  22. self.assertEqual(output, 'Included')
  23. @setup({'include03': '{% include template_name %}'}, basic_templates)
  24. def test_include03(self):
  25. output = self.engine.render_to_string(
  26. 'include03',
  27. {'template_name': 'basic-syntax02', 'headline': 'Included'},
  28. )
  29. self.assertEqual(output, 'Included')
  30. @setup({'include04': 'a{% include "nonexistent" %}b'})
  31. def test_include04(self):
  32. template = self.engine.get_template('include04')
  33. if self.engine.debug:
  34. with self.assertRaises(TemplateDoesNotExist):
  35. template.render(Context({}))
  36. else:
  37. with warnings.catch_warnings(record=True) as warns:
  38. warnings.simplefilter('always')
  39. output = template.render(Context({}))
  40. self.assertEqual(output, "ab")
  41. self.assertEqual(len(warns), 1)
  42. self.assertEqual(
  43. str(warns[0].message),
  44. "Rendering {% include 'include04' %} raised "
  45. "TemplateDoesNotExist. In Django 2.1, this exception will be "
  46. "raised rather than silenced and rendered as an empty string.",
  47. )
  48. @setup({
  49. 'include 05': 'template with a space',
  50. 'include06': '{% include "include 05"%}',
  51. })
  52. def test_include06(self):
  53. output = self.engine.render_to_string('include06')
  54. self.assertEqual(output, "template with a space")
  55. @setup({'include07': '{% include "basic-syntax02" with headline="Inline" %}'}, basic_templates)
  56. def test_include07(self):
  57. output = self.engine.render_to_string('include07', {'headline': 'Included'})
  58. self.assertEqual(output, 'Inline')
  59. @setup({'include08': '{% include headline with headline="Dynamic" %}'}, basic_templates)
  60. def test_include08(self):
  61. output = self.engine.render_to_string('include08', {'headline': 'basic-syntax02'})
  62. self.assertEqual(output, 'Dynamic')
  63. @setup(
  64. {'include09': '{{ first }}--'
  65. '{% include "basic-syntax03" with first=second|lower|upper second=first|upper %}'
  66. '--{{ second }}'},
  67. basic_templates,
  68. )
  69. def test_include09(self):
  70. output = self.engine.render_to_string('include09', {'first': 'Ul', 'second': 'lU'})
  71. self.assertEqual(output, 'Ul--LU --- UL--lU')
  72. @setup({'include10': '{% include "basic-syntax03" only %}'}, basic_templates)
  73. def test_include10(self):
  74. output = self.engine.render_to_string('include10', {'first': '1'})
  75. if self.engine.string_if_invalid:
  76. self.assertEqual(output, 'INVALID --- INVALID')
  77. else:
  78. self.assertEqual(output, ' --- ')
  79. @setup({'include11': '{% include "basic-syntax03" only with second=2 %}'}, basic_templates)
  80. def test_include11(self):
  81. output = self.engine.render_to_string('include11', {'first': '1'})
  82. if self.engine.string_if_invalid:
  83. self.assertEqual(output, 'INVALID --- 2')
  84. else:
  85. self.assertEqual(output, ' --- 2')
  86. @setup({'include12': '{% include "basic-syntax03" with first=1 only %}'}, basic_templates)
  87. def test_include12(self):
  88. output = self.engine.render_to_string('include12', {'second': '2'})
  89. if self.engine.string_if_invalid:
  90. self.assertEqual(output, '1 --- INVALID')
  91. else:
  92. self.assertEqual(output, '1 --- ')
  93. @setup(
  94. {'include13': '{% autoescape off %}{% include "basic-syntax03" %}{% endautoescape %}'},
  95. basic_templates,
  96. )
  97. def test_include13(self):
  98. output = self.engine.render_to_string('include13', {'first': '&'})
  99. if self.engine.string_if_invalid:
  100. self.assertEqual(output, '& --- INVALID')
  101. else:
  102. self.assertEqual(output, '& --- ')
  103. @setup(
  104. {'include14': '{% autoescape off %}'
  105. '{% include "basic-syntax03" with first=var1 only %}'
  106. '{% endautoescape %}'},
  107. basic_templates,
  108. )
  109. def test_include14(self):
  110. output = self.engine.render_to_string('include14', {'var1': '&'})
  111. if self.engine.string_if_invalid:
  112. self.assertEqual(output, '& --- INVALID')
  113. else:
  114. self.assertEqual(output, '& --- ')
  115. # Include syntax errors
  116. @setup({'include-error01': '{% include "basic-syntax01" with %}'})
  117. def test_include_error01(self):
  118. with self.assertRaises(TemplateSyntaxError):
  119. self.engine.get_template('include-error01')
  120. @setup({'include-error02': '{% include "basic-syntax01" with "no key" %}'})
  121. def test_include_error02(self):
  122. with self.assertRaises(TemplateSyntaxError):
  123. self.engine.get_template('include-error02')
  124. @setup({'include-error03': '{% include "basic-syntax01" with dotted.arg="error" %}'})
  125. def test_include_error03(self):
  126. with self.assertRaises(TemplateSyntaxError):
  127. self.engine.get_template('include-error03')
  128. @setup({'include-error04': '{% include "basic-syntax01" something_random %}'})
  129. def test_include_error04(self):
  130. with self.assertRaises(TemplateSyntaxError):
  131. self.engine.get_template('include-error04')
  132. @setup({'include-error05': '{% include "basic-syntax01" foo="duplicate" foo="key" %}'})
  133. def test_include_error05(self):
  134. with self.assertRaises(TemplateSyntaxError):
  135. self.engine.get_template('include-error05')
  136. @setup({'include-error06': '{% include "basic-syntax01" only only %}'})
  137. def test_include_error06(self):
  138. with self.assertRaises(TemplateSyntaxError):
  139. self.engine.get_template('include-error06')
  140. @setup(include_fail_templates)
  141. def test_include_fail1(self):
  142. with self.assertRaises(RuntimeError):
  143. self.engine.get_template('include-fail1')
  144. @setup(include_fail_templates)
  145. def test_include_fail2(self):
  146. with self.assertRaises(TemplateSyntaxError):
  147. self.engine.get_template('include-fail2')
  148. @setup({'include-error07': '{% include "include-fail1" %}'}, include_fail_templates)
  149. def test_include_error07(self):
  150. template = self.engine.get_template('include-error07')
  151. if self.engine.debug:
  152. with self.assertRaises(RuntimeError):
  153. template.render(Context())
  154. else:
  155. with ignore_warnings(category=RemovedInDjango21Warning):
  156. self.assertEqual(template.render(Context()), '')
  157. @setup({'include-error08': '{% include "include-fail2" %}'}, include_fail_templates)
  158. def test_include_error08(self):
  159. template = self.engine.get_template('include-error08')
  160. if self.engine.debug:
  161. with self.assertRaises(TemplateSyntaxError):
  162. template.render(Context())
  163. else:
  164. with ignore_warnings(category=RemovedInDjango21Warning):
  165. self.assertEqual(template.render(Context()), '')
  166. @setup({'include-error09': '{% include failed_include %}'}, include_fail_templates)
  167. def test_include_error09(self):
  168. context = Context({'failed_include': 'include-fail1'})
  169. template = self.engine.get_template('include-error09')
  170. if self.engine.debug:
  171. with self.assertRaises(RuntimeError):
  172. template.render(context)
  173. else:
  174. with ignore_warnings(category=RemovedInDjango21Warning):
  175. self.assertEqual(template.render(context), '')
  176. @setup({'include-error10': '{% include failed_include %}'}, include_fail_templates)
  177. def test_include_error10(self):
  178. context = Context({'failed_include': 'include-fail2'})
  179. template = self.engine.get_template('include-error10')
  180. if self.engine.debug:
  181. with self.assertRaises(TemplateSyntaxError):
  182. template.render(context)
  183. else:
  184. with ignore_warnings(category=RemovedInDjango21Warning):
  185. self.assertEqual(template.render(context), '')
  186. class IncludeTests(SimpleTestCase):
  187. def test_include_missing_template(self):
  188. """
  189. The correct template is identified as not existing
  190. when {% include %} specifies a template that does not exist.
  191. """
  192. engine = Engine(app_dirs=True, debug=True)
  193. template = engine.get_template('test_include_error.html')
  194. with self.assertRaises(TemplateDoesNotExist) as e:
  195. template.render(Context())
  196. self.assertEqual(e.exception.args[0], 'missing.html')
  197. def test_extends_include_missing_baseloader(self):
  198. """
  199. #12787 -- The correct template is identified as not existing
  200. when {% extends %} specifies a template that does exist, but that
  201. template has an {% include %} of something that does not exist.
  202. """
  203. engine = Engine(app_dirs=True, debug=True)
  204. template = engine.get_template('test_extends_error.html')
  205. with self.assertRaises(TemplateDoesNotExist) as e:
  206. template.render(Context())
  207. self.assertEqual(e.exception.args[0], 'missing.html')
  208. def test_extends_include_missing_cachedloader(self):
  209. engine = Engine(debug=True, loaders=[
  210. ('django.template.loaders.cached.Loader', [
  211. 'django.template.loaders.app_directories.Loader',
  212. ]),
  213. ])
  214. template = engine.get_template('test_extends_error.html')
  215. with self.assertRaises(TemplateDoesNotExist) as e:
  216. template.render(Context())
  217. self.assertEqual(e.exception.args[0], 'missing.html')
  218. # Repeat to ensure it still works when loading from the cache
  219. template = engine.get_template('test_extends_error.html')
  220. with self.assertRaises(TemplateDoesNotExist) as e:
  221. template.render(Context())
  222. self.assertEqual(e.exception.args[0], 'missing.html')
  223. def test_include_template_argument(self):
  224. """
  225. Support any render() supporting object
  226. """
  227. engine = Engine()
  228. ctx = Context({
  229. 'tmpl': engine.from_string('This worked!'),
  230. })
  231. outer_tmpl = engine.from_string('{% include tmpl %}')
  232. output = outer_tmpl.render(ctx)
  233. self.assertEqual(output, 'This worked!')
  234. def test_include_from_loader_get_template(self):
  235. tmpl = loader.get_template('include_tpl.html') # {% include tmpl %}
  236. output = tmpl.render({'tmpl': loader.get_template('index.html')})
  237. self.assertEqual(output, 'index\n\n')
  238. def test_include_immediate_missing(self):
  239. """
  240. #16417 -- Include tags pointing to missing templates should not raise
  241. an error at parsing time.
  242. """
  243. Engine(debug=True).from_string('{% include "this_does_not_exist.html" %}')
  244. def test_include_recursive(self):
  245. comments = [
  246. {
  247. 'comment': 'A1',
  248. 'children': [
  249. {'comment': 'B1', 'children': []},
  250. {'comment': 'B2', 'children': []},
  251. {'comment': 'B3', 'children': [
  252. {'comment': 'C1', 'children': []}
  253. ]},
  254. ]
  255. }
  256. ]
  257. engine = Engine(app_dirs=True)
  258. t = engine.get_template('recursive_include.html')
  259. self.assertEqual(
  260. "Recursion! A1 Recursion! B1 B2 B3 Recursion! C1",
  261. t.render(Context({'comments': comments})).replace(' ', '').replace('\n', ' ').strip(),
  262. )
  263. def test_include_cache(self):
  264. """
  265. {% include %} keeps resolved templates constant (#27974). The
  266. CounterNode object in the {% counter %} template tag is created once
  267. if caching works properly. Each iteration increases the counter instead
  268. of restarting it.
  269. This works as a regression test only if the cached loader
  270. isn't used, so the @setup decorator isn't used.
  271. """
  272. engine = Engine(loaders=[
  273. ('django.template.loaders.locmem.Loader', {
  274. 'template': '{% for x in vars %}{% include "include" %}{% endfor %}',
  275. 'include': '{% include "next" %}',
  276. 'next': '{% load custom %}{% counter %}'
  277. }),
  278. ], libraries={'custom': 'template_tests.templatetags.custom'})
  279. output = engine.render_to_string('template', {'vars': range(9)})
  280. self.assertEqual(output, '012345678')