Fixed #33134 -- Fixed recursion depth error when rendering Form with BoundFields.

Regression in 456466d932.
This commit is contained in:
David Smith 2021-09-26 19:55:04 +01:00 committed by Mariusz Felisiak
parent b7fd668b37
commit 4884a87e02
5 changed files with 34 additions and 1 deletions

View File

@ -177,7 +177,6 @@ class BoundField:
else:
attrs['class'] = self.form.required_css_class
context = {
'form': self.form,
'field': self,
'label': contents,
'attrs': attrs,

View File

@ -0,0 +1,2 @@
{% load tags %}
{% count_render %}

View File

@ -0,0 +1,21 @@
from django.template import Library, Node
register = Library()
class CountRenderNode(Node):
count = 0
def render(self, context):
self.count += 1
for v in context.flatten().values():
try:
v.render()
except AttributeError:
pass
return str(self.count)
@register.tag
def count_render(parser, token):
return CountRenderNode()

View File

@ -3954,3 +3954,14 @@ class OverrideTests(SimpleTestCase):
'<div class="error">This field is required.</div></div>'
'<p>Comment: <input type="text" name="comment" required></p>',
)
def test_cyclic_context_boundfield_render(self):
class FirstNameForm(Form):
first_name = CharField()
template_name_label = 'forms_tests/cyclic_context_boundfield_render.html'
f = FirstNameForm()
try:
self.assertInHTML('<th>1</th>', f.render())
except RecursionError:
self.fail('Cyclic reference in BoundField.render().')