Skip to content

Commit e643ba8

Browse files
authored
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.
1 parent f42c7cc commit e643ba8

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

django/template/context.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,17 @@ def __getitem__(self, key):
203203
return self.dicts[-1][key]
204204

205205
@contextmanager
206-
def push_state(self, template):
206+
def push_state(self, template, isolated_context=True):
207207
initial = self.template
208208
self.template = template
209-
self.push()
209+
if isolated_context:
210+
self.push()
210211
try:
211212
yield
212213
finally:
213214
self.template = initial
214-
self.pop()
215+
if isolated_context:
216+
self.pop()
215217

216218

217219
class RequestContext(Context):

django/template/loader_tags.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def render(self, context):
151151

152152
# Call Template._render explicitly so the parser context stays
153153
# the same.
154-
return compiled_parent._render(context)
154+
with context.render_context.push_state(compiled_parent, isolated_context=False):
155+
return compiled_parent._render(context)
155156

156157

157158
class IncludeNode(Node):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% extends "27956_parent.html" %}
2+
3+
{% block content %}{% endblock %}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% load tag_27584 %}
2+
3+
{% badtag %}{% endbadtag %}
4+
5+
{% block content %}{% endblock %}

tests/template_tests/tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ def test_compile_tag_error_27584(self):
122122
t.render(Context())
123123
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
124124

125+
def test_compile_tag_error_27956(self):
126+
"""Errors in a child of {% extends %} are displayed correctly."""
127+
engine = Engine(
128+
app_dirs=True,
129+
debug=True,
130+
libraries={'tag_27584': 'template_tests.templatetags.tag_27584'},
131+
)
132+
t = engine.get_template('27956_child.html')
133+
with self.assertRaises(TemplateSyntaxError) as e:
134+
t.render(Context())
135+
self.assertEqual(e.exception.template_debug['during'], '{% badtag %}')
136+
125137
def test_super_errors(self):
126138
"""
127139
#18169 -- NoReverseMatch should not be silence in block.super.

0 commit comments

Comments
 (0)