From e643ba8bcf0b1da517cbab689ac157ee031202a3 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 25 Mar 2017 08:22:12 -0400 Subject: [PATCH] Fixed #27956 -- Fixed display of errors in an {% extends %} child. Thanks Ling-Xiao Yang for the report and test, and Preston Timmons for the fix. --- django/template/context.py | 8 +++++--- django/template/loader_tags.py | 3 ++- tests/template_tests/templates/27956_child.html | 3 +++ tests/template_tests/templates/27956_parent.html | 5 +++++ tests/template_tests/tests.py | 12 ++++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/template_tests/templates/27956_child.html create mode 100644 tests/template_tests/templates/27956_parent.html diff --git a/django/template/context.py b/django/template/context.py index 8cb82ed80fc..f0435b7e8c7 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -203,15 +203,17 @@ class RenderContext(BaseContext): return self.dicts[-1][key] @contextmanager - def push_state(self, template): + def push_state(self, template, isolated_context=True): initial = self.template self.template = template - self.push() + if isolated_context: + self.push() try: yield finally: self.template = initial - self.pop() + if isolated_context: + self.pop() class RequestContext(Context): diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py index d043e5eb529..0f70a396553 100644 --- a/django/template/loader_tags.py +++ b/django/template/loader_tags.py @@ -151,7 +151,8 @@ class ExtendsNode(Node): # Call Template._render explicitly so the parser context stays # the same. - return compiled_parent._render(context) + with context.render_context.push_state(compiled_parent, isolated_context=False): + return compiled_parent._render(context) class IncludeNode(Node): diff --git a/tests/template_tests/templates/27956_child.html b/tests/template_tests/templates/27956_child.html new file mode 100644 index 00000000000..ea9aeb21978 --- /dev/null +++ b/tests/template_tests/templates/27956_child.html @@ -0,0 +1,3 @@ +{% extends "27956_parent.html" %} + +{% block content %}{% endblock %} diff --git a/tests/template_tests/templates/27956_parent.html b/tests/template_tests/templates/27956_parent.html new file mode 100644 index 00000000000..dad4fbbfe5e --- /dev/null +++ b/tests/template_tests/templates/27956_parent.html @@ -0,0 +1,5 @@ +{% load tag_27584 %} + +{% badtag %}{% endbadtag %} + +{% block content %}{% endblock %} diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index 3ab215c094c..a8f089c3511 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -122,6 +122,18 @@ class TemplateTests(SimpleTestCase): t.render(Context()) self.assertEqual(e.exception.template_debug['during'], '{% badtag %}') + def test_compile_tag_error_27956(self): + """Errors in a child of {% extends %} are displayed correctly.""" + engine = Engine( + app_dirs=True, + debug=True, + libraries={'tag_27584': 'template_tests.templatetags.tag_27584'}, + ) + t = engine.get_template('27956_child.html') + with self.assertRaises(TemplateSyntaxError) as e: + t.render(Context()) + self.assertEqual(e.exception.template_debug['during'], '{% badtag %}') + def test_super_errors(self): """ #18169 -- NoReverseMatch should not be silence in block.super.