Commit 3f9ec12d authored by Yang Liu's avatar Yang Liu Committed by Tim Graham
Browse files

Fixed #23712 -- Fixed KeyError with BaseForm._html_output()

parent faf0d66a
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -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:
+54 −0
Original line number Diff line number Diff line
@@ -2191,6 +2191,60 @@ class FormsTestCase(TestCase):
        form = SomeForm()
        self.assertHTMLEqual(form.as_p(), '<p id="p_some_field"></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='<p%(html_class_attr)s>%(field)s %(field_name)s</p>',
                    error_row='%s',
                    row_ender='</p>',
                    help_text_html=' %s',
                    errors_on_separate_row=True,
                )

        form = SomeForm()
        self.assertHTMLEqual(
            form.as_p(),
            '<p><input id="id_custom" name="custom" type="text" /> custom'
            '<input id="id_hidden1" name="hidden1" type="hidden" />'
            '<input id="id_hidden2" name="hidden2" type="hidden" /></p>'
        )

    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='<p%(html_class_attr)s>%(field)s %(field_name)s</p>',
                    error_row='%s',
                    row_ender='<hr/><hr/>',
                    help_text_html=' %s',
                    errors_on_separate_row=True
                )

        form = SomeForm()
        self.assertHTMLEqual(
            form.as_p(),
            '<p><input id="id_custom" name="custom" type="text" /> custom</p>\n'
            '<input id="id_hidden1" name="hidden1" type="hidden" />'
            '<input id="id_hidden2" name="hidden2" type="hidden" /><hr/><hr/>'
        )

    def test_error_dict(self):
        class MyForm(Form):
            foo = CharField()