diff options
| author | David Smith <smithdc@gmail.com> | 2021-09-26 19:55:04 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-09-29 10:55:01 +0200 |
| commit | 0b62518ff424b0fe442b41636871fd99832ebdcb (patch) | |
| tree | bc8ec8d4bcca752259f655abc879dce5a7951b6f | |
| parent | 6f310417941182fda01f5276462ad8538d5f77d8 (diff) | |
[4.0.x] Fixed #33134 -- Fixed recursion depth error when rendering Form with BoundFields.
Regression in 456466d932830b096d39806e291fe23ec5ed38d5.
Backport of 4884a87e022056eda10534c13d74e49b8cdda632 from main
| -rw-r--r-- | django/forms/boundfield.py | 1 | ||||
| -rw-r--r-- | tests/forms_tests/templates/forms_tests/cyclic_context_boundfield_render.html | 2 | ||||
| -rw-r--r-- | tests/forms_tests/templatetags/__init__.py | 0 | ||||
| -rw-r--r-- | tests/forms_tests/templatetags/tags.py | 21 | ||||
| -rw-r--r-- | tests/forms_tests/tests/test_forms.py | 11 |
5 files changed, 34 insertions, 1 deletions
diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index d1e98719d2..5bbfcbe41c 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 0000000000..726e82a3a8 --- /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 0000000000..e69de29bb2 --- /dev/null +++ b/tests/forms_tests/templatetags/__init__.py diff --git a/tests/forms_tests/templatetags/tags.py b/tests/forms_tests/templatetags/tags.py new file mode 100644 index 0000000000..060e5008da --- /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 fefebde05c..c478a71699 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -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().') |
