diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 7f7d56ba1f..9a1257295e 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -75,20 +75,28 @@ class Form(StrAndUnicode): def as_table(self): "Returns this form rendered as HTML s -- excluding the
." top_errors = self.non_field_errors() - output = [] + output, hidden_fields = [], [] for name, field in self.fields.items(): bf = BoundField(self, field, name) bf_errors = bf.errors # Cache in local variable. if bf.is_hidden: if bf_errors: top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors]) - output.append(str(bf)) + hidden_fields.append(str(bf)) else: if bf_errors: output.append(u'%s' % bf_errors) output.append(u'%s%s' % (bf.label_tag(escape(bf.verbose_name+':')), bf)) if top_errors: output.insert(0, u'%s' % top_errors) + if hidden_fields: # Insert any hidden fields in the last . + str_hidden = u''.join(hidden_fields) + if output: + last_td = output[-1] + # Chop off the trailing '' and insert the hidden fields. + output[-1] = last_td[:-10] + str_hidden + '' + else: # If there aren't any ''s in the output, just append the hidden fields. + output.append(str_hidden) return u'\n'.join(output) def as_ul(self): diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 572d1a1f7f..db3ee6124d 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1756,7 +1756,8 @@ subclass' __init__(). HiddenInput widgets are displayed differently in the as_table() and as_ul() output of a Form -- their verbose names are not displayed, and a separate -/
  • is not displayed. +/
  • is not displayed. They're displayed in the last of the form, +directly after that 's form element. >>> class Person(Form): ... first_name = CharField() ... last_name = CharField() @@ -1766,8 +1767,7 @@ output of a Form -- their verbose names are not displayed, and a separate >>> print p First name: Last name: - -Birthday: +Birthday: >>> print p.as_ul()
  • First name:
  • Last name:
  • @@ -1779,8 +1779,7 @@ With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label. >>> print p - - + >>> print p.as_ul()
  • @@ -1796,8 +1795,7 @@ its field's order in the form. First name: Last name: - -Birthday: +Birthday: >>> print p.as_ul()
  • First name:
  • @@ -1805,6 +1803,17 @@ its field's order in the form.
  • Birthday:
  • +A corner case: It's possible for a form to have only HiddenInputs. +>>> class TestForm(Form): +... foo = CharField(widget=HiddenInput) +... bar = CharField(widget=HiddenInput) +>>> p = TestForm() +>>> print p.as_table() + +>>> print p.as_ul() + + + A Form's fields are displayed in the same order in which they were defined. >>> class TestForm(Form): ... field1 = CharField()