From 9658f0d266bfded9ead5ead7a80823b4739c149f Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 11 Feb 2017 06:51:57 -0500 Subject: [PATCH] [1.11.x] Fixed #27722 -- Reallowed using django.Template in {% include %}. Backport of fe2d2884345e1e6a1daadd76e9404c62791ab589 from master --- django/template/loader_tags.py | 3 +++ tests/template_tests/syntax_tests/test_include.py | 7 ++++++- tests/template_tests/templates/include_tpl.html | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/template_tests/templates/include_tpl.html diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py index 2ed145c248..4ff5ec4db5 100644 --- a/django/template/loader_tags.py +++ b/django/template/loader_tags.py @@ -202,6 +202,9 @@ class IncludeNode(Node): if template is None: template = context.template.engine.get_template(template_name) cache[template_name] = template + # Use the base.Template of a backends.django.Template. + elif hasattr(template, 'template'): + template = template.template values = { name: var.resolve(context) for name, var in six.iteritems(self.extra_context) diff --git a/tests/template_tests/syntax_tests/test_include.py b/tests/template_tests/syntax_tests/test_include.py index ca7ee6f5b0..185bcb3988 100644 --- a/tests/template_tests/syntax_tests/test_include.py +++ b/tests/template_tests/syntax_tests/test_include.py @@ -1,7 +1,7 @@ import warnings from django.template import ( - Context, Engine, TemplateDoesNotExist, TemplateSyntaxError, + Context, Engine, TemplateDoesNotExist, TemplateSyntaxError, loader, ) from django.test import SimpleTestCase, ignore_warnings from django.utils.deprecation import RemovedInDjango21Warning @@ -280,6 +280,11 @@ class IncludeTests(SimpleTestCase): output = outer_tmpl.render(ctx) self.assertEqual(output, 'This worked!') + def test_include_from_loader_get_template(self): + tmpl = loader.get_template('include_tpl.html') # {% include tmpl %} + output = tmpl.render({'tmpl': loader.get_template('index.html')}) + self.assertEqual(output, 'index\n\n') + def test_include_immediate_missing(self): """ #16417 -- Include tags pointing to missing templates should not raise diff --git a/tests/template_tests/templates/include_tpl.html b/tests/template_tests/templates/include_tpl.html new file mode 100644 index 0000000000..7a8374df51 --- /dev/null +++ b/tests/template_tests/templates/include_tpl.html @@ -0,0 +1 @@ +{% include tmpl %}