newforms: Form.as_ul() no longer puts hidden fields between <li>s. Similar to [4175], which was the same thing for Form.as_table(). Refs #3101
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4177 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
3d1ceab1aa
commit
44add112e5
|
@ -102,18 +102,26 @@ class Form(StrAndUnicode):
|
||||||
def as_ul(self):
|
def as_ul(self):
|
||||||
"Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
|
"Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
|
||||||
top_errors = self.non_field_errors()
|
top_errors = self.non_field_errors()
|
||||||
output = []
|
output, hidden_fields = [], []
|
||||||
for name, field in self.fields.items():
|
for name, field in self.fields.items():
|
||||||
bf = BoundField(self, field, name)
|
bf = BoundField(self, field, name)
|
||||||
if bf.is_hidden:
|
if bf.is_hidden:
|
||||||
new_errors = bf.errors # Cache in local variable.
|
new_errors = bf.errors # Cache in local variable.
|
||||||
if new_errors:
|
if new_errors:
|
||||||
top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in 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:
|
else:
|
||||||
output.append(u'<li>%s%s %s</li>' % (bf.errors, bf.label_tag(escape(bf.verbose_name+':')), bf))
|
output.append(u'<li>%s%s %s</li>' % (bf.errors, bf.label_tag(escape(bf.verbose_name+':')), bf))
|
||||||
if top_errors:
|
if top_errors:
|
||||||
output.insert(0, u'<li>%s</li>' % top_errors)
|
output.insert(0, u'<li>%s</li>' % top_errors)
|
||||||
|
if hidden_fields: # Insert any hidden fields in the last <li>.
|
||||||
|
str_hidden = u''.join(hidden_fields)
|
||||||
|
if output:
|
||||||
|
last_li = output[-1]
|
||||||
|
# Chop off the trailing '</li>' and insert the hidden fields.
|
||||||
|
output[-1] = last_li[:-5] + str_hidden + '</li>'
|
||||||
|
else: # If there aren't any '<li>'s in the output, just append the hidden fields.
|
||||||
|
output.append(str_hidden)
|
||||||
return u'\n'.join(output)
|
return u'\n'.join(output)
|
||||||
|
|
||||||
def non_field_errors(self):
|
def non_field_errors(self):
|
||||||
|
|
|
@ -1756,8 +1756,8 @@ subclass' __init__().
|
||||||
|
|
||||||
HiddenInput widgets are displayed differently in the as_table() and as_ul()
|
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
|
output of a Form -- their verbose names are not displayed, and a separate
|
||||||
<tr>/<li> is not displayed. They're displayed in the last <td> of the form,
|
<tr>/<li> is not displayed. They're displayed in the last <td>/<li> of the
|
||||||
directly after that <td>'s form element.
|
form, directly after that row's form element.
|
||||||
>>> class Person(Form):
|
>>> class Person(Form):
|
||||||
... first_name = CharField()
|
... first_name = CharField()
|
||||||
... last_name = CharField()
|
... last_name = CharField()
|
||||||
|
@ -1771,8 +1771,7 @@ directly after that <td>'s form element.
|
||||||
>>> print p.as_ul()
|
>>> print p.as_ul()
|
||||||
<li>First name: <input type="text" name="first_name" /></li>
|
<li>First name: <input type="text" name="first_name" /></li>
|
||||||
<li>Last name: <input type="text" name="last_name" /></li>
|
<li>Last name: <input type="text" name="last_name" /></li>
|
||||||
<input type="hidden" name="hidden_text" />
|
<li>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></li>
|
||||||
<li>Birthday: <input type="text" name="birthday" /></li>
|
|
||||||
|
|
||||||
With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
|
With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
|
||||||
>>> p = Person(auto_id='id_%s')
|
>>> 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()
|
>>> print p.as_ul()
|
||||||
<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
|
<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
|
||||||
<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
|
<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
|
||||||
<input type="hidden" name="hidden_text" id="id_hidden_text" />
|
<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></li>
|
||||||
<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>
|
|
||||||
|
|
||||||
If a field with a HiddenInput has errors, the as_table() and as_ul() output
|
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]) "
|
will include the error message(s) with the text "(Hidden field [fieldname]) "
|
||||||
|
@ -1800,8 +1798,7 @@ its field's order in the form.
|
||||||
<li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
|
<li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
|
||||||
<li>First name: <input type="text" name="first_name" value="John" /></li>
|
<li>First name: <input type="text" name="first_name" value="John" /></li>
|
||||||
<li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
|
<li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
|
||||||
<input type="hidden" name="hidden_text" />
|
<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li>
|
||||||
<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li>
|
|
||||||
|
|
||||||
A corner case: It's possible for a form to have only HiddenInputs.
|
A corner case: It's possible for a form to have only HiddenInputs.
|
||||||
>>> class TestForm(Form):
|
>>> 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_table()
|
||||||
<input type="hidden" name="foo" /><input type="hidden" name="bar" />
|
<input type="hidden" name="foo" /><input type="hidden" name="bar" />
|
||||||
>>> print p.as_ul()
|
>>> print p.as_ul()
|
||||||
<input type="hidden" name="foo" />
|
<input type="hidden" name="foo" /><input type="hidden" name="bar" />
|
||||||
<input type="hidden" name="bar" />
|
|
||||||
|
|
||||||
A Form's fields are displayed in the same order in which they were defined.
|
A Form's fields are displayed in the same order in which they were defined.
|
||||||
>>> class TestForm(Form):
|
>>> class TestForm(Form):
|
||||||
|
|
Loading…
Reference in New Issue