From 3f9ec12d9c9eff9a3b1a205d87c7e66587cf9967 Mon Sep 17 00:00:00 2001
From: Yang Liu
Date: Tue, 6 Jan 2015 17:07:40 +0800
Subject: [PATCH] Fixed #23712 -- Fixed KeyError with BaseForm._html_output()
---
django/forms/forms.py | 11 ++++--
tests/forms_tests/tests/test_forms.py | 54 +++++++++++++++++++++++++++
2 files changed, 62 insertions(+), 3 deletions(-)
diff --git a/django/forms/forms.py b/django/forms/forms.py
index e68542e12a..84070d95c6 100644
--- a/django/forms/forms.py
+++ b/django/forms/forms.py
@@ -251,9 +251,14 @@ class BaseForm(object):
# that users write): if there are only top errors, we may
# not be able to conscript the last row for our purposes,
# so insert a new, empty row.
- last_row = (normal_row % {'errors': '', 'label': '',
- 'field': '', 'help_text': '',
- 'html_class_attr': html_class_attr})
+ last_row = (normal_row % {
+ 'errors': '',
+ 'label': '',
+ 'field': '',
+ 'help_text': '',
+ 'html_class_attr': html_class_attr,
+ 'field_name': '',
+ })
output.append(last_row)
output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
else:
diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py
index 5b1c85ce6a..c0ce912fe1 100644
--- a/tests/forms_tests/tests/test_forms.py
+++ b/tests/forms_tests/tests/test_forms.py
@@ -2191,6 +2191,60 @@ class FormsTestCase(TestCase):
form = SomeForm()
self.assertHTMLEqual(form.as_p(), '
')
+ def test_field_name_with_hidden_input(self):
+ """
+ BaseForm._html_output() should merge all the hidden input fields and
+ put them in the last row.
+ """
+ class SomeForm(Form):
+ hidden1 = CharField(widget=HiddenInput)
+ custom = CharField()
+ hidden2 = CharField(widget=HiddenInput)
+
+ def as_p(self):
+ return self._html_output(
+ normal_row='%(field)s %(field_name)s
',
+ error_row='%s',
+ row_ender='
',
+ help_text_html=' %s',
+ errors_on_separate_row=True,
+ )
+
+ form = SomeForm()
+ self.assertHTMLEqual(
+ form.as_p(),
+ ' custom'
+ ' '
+ '
'
+ )
+
+ def test_field_name_with_hidden_input_and_non_matching_row_ender(self):
+ """
+ BaseForm._html_output() should merge all the hidden input fields and
+ put them in the last row ended with the specific row ender.
+ """
+ class SomeForm(Form):
+ hidden1 = CharField(widget=HiddenInput)
+ custom = CharField()
+ hidden2 = CharField(widget=HiddenInput)
+
+ def as_p(self):
+ return self._html_output(
+ normal_row='%(field)s %(field_name)s
',
+ error_row='%s',
+ row_ender=' ',
+ help_text_html=' %s',
+ errors_on_separate_row=True
+ )
+
+ form = SomeForm()
+ self.assertHTMLEqual(
+ form.as_p(),
+ ' custom
\n'
+ ' '
+ ' '
+ )
+
def test_error_dict(self):
class MyForm(Form):
foo = CharField()