From 4884a87e022056eda10534c13d74e49b8cdda632 Mon Sep 17 00:00:00 2001 From: David Smith Date: Sun, 26 Sep 2021 19:55:04 +0100 Subject: [PATCH] Fixed #33134 -- Fixed recursion depth error when rendering Form with BoundFields. Regression in 456466d932830b096d39806e291fe23ec5ed38d5. --- django/forms/boundfield.py | 1 - .../cyclic_context_boundfield_render.html | 2 ++ tests/forms_tests/templatetags/__init__.py | 0 tests/forms_tests/templatetags/tags.py | 21 +++++++++++++++++++ tests/forms_tests/tests/test_forms.py | 11 ++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/forms_tests/templates/forms_tests/cyclic_context_boundfield_render.html create mode 100644 tests/forms_tests/templatetags/__init__.py create mode 100644 tests/forms_tests/templatetags/tags.py diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index d1e98719d25..5bbfcbe41c6 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -177,7 +177,6 @@ class BoundField: else: attrs['class'] = self.form.required_css_class context = { - 'form': self.form, 'field': self, 'label': contents, 'attrs': attrs, diff --git a/tests/forms_tests/templates/forms_tests/cyclic_context_boundfield_render.html b/tests/forms_tests/templates/forms_tests/cyclic_context_boundfield_render.html new file mode 100644 index 00000000000..726e82a3a89 --- /dev/null +++ b/tests/forms_tests/templates/forms_tests/cyclic_context_boundfield_render.html @@ -0,0 +1,2 @@ +{% load tags %} +{% count_render %} diff --git a/tests/forms_tests/templatetags/__init__.py b/tests/forms_tests/templatetags/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/forms_tests/templatetags/tags.py b/tests/forms_tests/templatetags/tags.py new file mode 100644 index 00000000000..060e5008da5 --- /dev/null +++ b/tests/forms_tests/templatetags/tags.py @@ -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() diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index fefebde05c9..c478a71699f 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -3954,3 +3954,14 @@ class OverrideTests(SimpleTestCase): '
This field is required.
' '

Comment:

', ) + + 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('1', f.render()) + except RecursionError: + self.fail('Cyclic reference in BoundField.render().')