diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 9e4fc30a7a..36dd36fcce 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -102,18 +102,26 @@ class Form(StrAndUnicode):
def as_ul(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)
if bf.is_hidden:
new_errors = bf.errors # Cache in local variable.
if new_errors:
top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in new_errors])
- output.append(unicode(bf))
+ hidden_fields.append(unicode(bf))
else:
output.append(u'
%s%s %s
' % (bf.errors, 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_li = output[-1]
+ # Chop off the trailing '
' and insert the hidden fields.
+ output[-1] = last_li[:-5] + 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 non_field_errors(self):
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index db3ee6124d..16e56a8212 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -1756,8 +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. They're displayed in the last
of the form,
-directly after that
's form element.
+
/
is not displayed. They're displayed in the last
/
of the
+form, directly after that row's form element.
>>> class Person(Form):
... first_name = CharField()
... last_name = CharField()
@@ -1771,8 +1771,7 @@ directly after that
's form element.
>>> print p.as_ul()
First name:
Last name:
-
-
Birthday:
+
Birthday:
With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
>>> p = Person(auto_id='id_%s')
@@ -1783,8 +1782,7 @@ With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
>>> print p.as_ul()
-
-
+
If a field with a HiddenInput has errors, the as_table() and as_ul() output
will include the error message(s) with the text "(Hidden field [fieldname]) "
@@ -1800,8 +1798,7 @@ its field's order in the form.
(Hidden field hidden_text) This field is required.
First name:
Last name:
-
-
Birthday:
+
Birthday:
A corner case: It's possible for a form to have only HiddenInputs.
>>> class TestForm(Form):
@@ -1811,8 +1808,7 @@ A corner case: It's possible for a form to have only HiddenInputs.
>>> 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):