newforms: Fixed #3027 -- Changed Form as_table(), as_ul(), as_table_with_errors() and as_ul_with_errors() to exclude <table> and <ul>. Good idea, SmileyChris
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4075 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
05420093ae
commit
75516392b8
|
@ -64,17 +64,15 @@ class Form(object):
|
||||||
return not bool(self.errors())
|
return not bool(self.errors())
|
||||||
|
|
||||||
def as_table(self):
|
def as_table(self):
|
||||||
"Returns this form rendered as an HTML <table>."
|
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
|
||||||
output = u'\n'.join(['<tr><td>%s:</td><td>%s</td></tr>' % (pretty_name(name), BoundField(self, field, name)) for name, field in self.fields.items()])
|
return u'\n'.join(['<tr><td>%s:</td><td>%s</td></tr>' % (pretty_name(name), BoundField(self, field, name)) for name, field in self.fields.items()])
|
||||||
return '<table>\n%s\n</table>' % output
|
|
||||||
|
|
||||||
def as_ul(self):
|
def as_ul(self):
|
||||||
"Returns this form rendered as an HTML <ul>."
|
"Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
|
||||||
output = u'\n'.join(['<li>%s: %s</li>' % (pretty_name(name), BoundField(self, field, name)) for name, field in self.fields.items()])
|
return u'\n'.join(['<li>%s: %s</li>' % (pretty_name(name), BoundField(self, field, name)) for name, field in self.fields.items()])
|
||||||
return '<ul>\n%s\n</ul>' % output
|
|
||||||
|
|
||||||
def as_table_with_errors(self):
|
def as_table_with_errors(self):
|
||||||
"Returns this form rendered as an HTML <table>, with errors."
|
"Returns this form rendered as HTML <tr>s, with errors."
|
||||||
output = []
|
output = []
|
||||||
if self.errors().get(NON_FIELD_ERRORS):
|
if self.errors().get(NON_FIELD_ERRORS):
|
||||||
# Errors not corresponding to a particular field are displayed at the top.
|
# Errors not corresponding to a particular field are displayed at the top.
|
||||||
|
@ -84,10 +82,10 @@ class Form(object):
|
||||||
if bf.errors:
|
if bf.errors:
|
||||||
output.append('<tr><td colspan="2"><ul>%s</ul></td></tr>' % '\n'.join(['<li>%s</li>' % e for e in bf.errors]))
|
output.append('<tr><td colspan="2"><ul>%s</ul></td></tr>' % '\n'.join(['<li>%s</li>' % e for e in bf.errors]))
|
||||||
output.append('<tr><td>%s:</td><td>%s</td></tr>' % (pretty_name(name), bf))
|
output.append('<tr><td>%s:</td><td>%s</td></tr>' % (pretty_name(name), bf))
|
||||||
return '<table>\n%s\n</table>' % '\n'.join(output)
|
return u'\n'.join(output)
|
||||||
|
|
||||||
def as_ul_with_errors(self):
|
def as_ul_with_errors(self):
|
||||||
"Returns this form rendered as an HTML <ul>, with errors."
|
"Returns this form rendered as HTML <li>s, with errors."
|
||||||
output = []
|
output = []
|
||||||
if self.errors().get(NON_FIELD_ERRORS):
|
if self.errors().get(NON_FIELD_ERRORS):
|
||||||
# Errors not corresponding to a particular field are displayed at the top.
|
# Errors not corresponding to a particular field are displayed at the top.
|
||||||
|
@ -99,7 +97,7 @@ class Form(object):
|
||||||
line += '<ul>%s</ul>' % '\n'.join(['<li>%s</li>' % e for e in bf.errors])
|
line += '<ul>%s</ul>' % '\n'.join(['<li>%s</li>' % e for e in bf.errors])
|
||||||
line += '%s: %s</li>' % (pretty_name(name), bf)
|
line += '%s: %s</li>' % (pretty_name(name), bf)
|
||||||
output.append(line)
|
output.append(line)
|
||||||
return '<ul>\n%s\n</ul>' % '\n'.join(output)
|
return u'\n'.join(output)
|
||||||
|
|
||||||
def full_clean(self):
|
def full_clean(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -881,38 +881,28 @@ ValidationError: [u'This field is required.']
|
||||||
... birthday = DateField()
|
... birthday = DateField()
|
||||||
>>> p = Person()
|
>>> p = Person()
|
||||||
>>> print p
|
>>> print p
|
||||||
<table>
|
|
||||||
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
|
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
|
||||||
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
|
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
|
||||||
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
|
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
|
||||||
</table>
|
|
||||||
>>> print p.as_table()
|
>>> print p.as_table()
|
||||||
<table>
|
|
||||||
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
|
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
|
||||||
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
|
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
|
||||||
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
|
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
|
||||||
</table>
|
|
||||||
>>> print p.as_ul()
|
>>> print p.as_ul()
|
||||||
<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>
|
||||||
<li>Birthday: <input type="text" name="birthday" /></li>
|
<li>Birthday: <input type="text" name="birthday" /></li>
|
||||||
</ul>
|
|
||||||
>>> print p.as_table_with_errors()
|
>>> print p.as_table_with_errors()
|
||||||
<table>
|
|
||||||
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr>
|
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr>
|
||||||
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
|
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
|
||||||
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr>
|
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr>
|
||||||
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
|
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
|
||||||
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr>
|
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr>
|
||||||
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
|
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
|
||||||
</table>
|
|
||||||
>>> print p.as_ul_with_errors()
|
>>> print p.as_ul_with_errors()
|
||||||
<ul>
|
|
||||||
<li><ul><li>This field is required.</li></ul>First name: <input type="text" name="first_name" /></li>
|
<li><ul><li>This field is required.</li></ul>First name: <input type="text" name="first_name" /></li>
|
||||||
<li><ul><li>This field is required.</li></ul>Last name: <input type="text" name="last_name" /></li>
|
<li><ul><li>This field is required.</li></ul>Last name: <input type="text" name="last_name" /></li>
|
||||||
<li><ul><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li>
|
<li><ul><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li>
|
||||||
</ul>
|
|
||||||
|
|
||||||
>>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'})
|
>>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'})
|
||||||
>>> p.errors()
|
>>> p.errors()
|
||||||
|
@ -937,11 +927,9 @@ u''
|
||||||
<input type="text" name="last_name" value="Lennon" />
|
<input type="text" name="last_name" value="Lennon" />
|
||||||
<input type="text" name="birthday" value="1940-10-9" />
|
<input type="text" name="birthday" value="1940-10-9" />
|
||||||
>>> print p
|
>>> print p
|
||||||
<table>
|
|
||||||
<tr><td>First name:</td><td><input type="text" name="first_name" value="John" /></td></tr>
|
<tr><td>First name:</td><td><input type="text" name="first_name" value="John" /></td></tr>
|
||||||
<tr><td>Last name:</td><td><input type="text" name="last_name" value="Lennon" /></td></tr>
|
<tr><td>Last name:</td><td><input type="text" name="last_name" value="Lennon" /></td></tr>
|
||||||
<tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr>
|
<tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr>
|
||||||
</table>
|
|
||||||
|
|
||||||
>>> p = Person({'last_name': u'Lennon'})
|
>>> p = Person({'last_name': u'Lennon'})
|
||||||
>>> p.errors()
|
>>> p.errors()
|
||||||
|
@ -978,31 +966,25 @@ If it's a string that contains '%s', Django will use that as a format string
|
||||||
into which the field's name will be inserted.
|
into which the field's name will be inserted.
|
||||||
>>> p = Person(auto_id='id_%s')
|
>>> p = Person(auto_id='id_%s')
|
||||||
>>> print p.as_ul()
|
>>> print p.as_ul()
|
||||||
<ul>
|
|
||||||
<li>First name: <input type="text" name="first_name" id="id_first_name" /></li>
|
<li>First name: <input type="text" name="first_name" id="id_first_name" /></li>
|
||||||
<li>Last name: <input type="text" name="last_name" id="id_last_name" /></li>
|
<li>Last name: <input type="text" name="last_name" id="id_last_name" /></li>
|
||||||
<li>Birthday: <input type="text" name="birthday" id="id_birthday" /></li>
|
<li>Birthday: <input type="text" name="birthday" id="id_birthday" /></li>
|
||||||
</ul>
|
|
||||||
|
|
||||||
If auto_id is any True value whose str() does not contain '%s', the "id"
|
If auto_id is any True value whose str() does not contain '%s', the "id"
|
||||||
attribute will be the name of the field.
|
attribute will be the name of the field.
|
||||||
>>> p = Person(auto_id=True)
|
>>> p = Person(auto_id=True)
|
||||||
>>> print p.as_ul()
|
>>> print p.as_ul()
|
||||||
<ul>
|
|
||||||
<li>First name: <input type="text" name="first_name" id="first_name" /></li>
|
<li>First name: <input type="text" name="first_name" id="first_name" /></li>
|
||||||
<li>Last name: <input type="text" name="last_name" id="last_name" /></li>
|
<li>Last name: <input type="text" name="last_name" id="last_name" /></li>
|
||||||
<li>Birthday: <input type="text" name="birthday" id="birthday" /></li>
|
<li>Birthday: <input type="text" name="birthday" id="birthday" /></li>
|
||||||
</ul>
|
|
||||||
|
|
||||||
If auto_id is any False value, an "id" attribute won't be output unless it
|
If auto_id is any False value, an "id" attribute won't be output unless it
|
||||||
was manually entered.
|
was manually entered.
|
||||||
>>> p = Person(auto_id=False)
|
>>> p = Person(auto_id=False)
|
||||||
>>> print p.as_ul()
|
>>> print p.as_ul()
|
||||||
<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>
|
||||||
<li>Birthday: <input type="text" name="birthday" /></li>
|
<li>Birthday: <input type="text" name="birthday" /></li>
|
||||||
</ul>
|
|
||||||
|
|
||||||
In this example, auto_id is False, but the "id" attribute for the "first_name"
|
In this example, auto_id is False, but the "id" attribute for the "first_name"
|
||||||
field is given.
|
field is given.
|
||||||
|
@ -1012,21 +994,17 @@ field is given.
|
||||||
... birthday = DateField()
|
... birthday = DateField()
|
||||||
>>> p = PersonNew(auto_id=False)
|
>>> p = PersonNew(auto_id=False)
|
||||||
>>> print p.as_ul()
|
>>> print p.as_ul()
|
||||||
<ul>
|
|
||||||
<li>First name: <input type="text" id="first_name_id" name="first_name" /></li>
|
<li>First name: <input type="text" id="first_name_id" name="first_name" /></li>
|
||||||
<li>Last name: <input type="text" name="last_name" /></li>
|
<li>Last name: <input type="text" name="last_name" /></li>
|
||||||
<li>Birthday: <input type="text" name="birthday" /></li>
|
<li>Birthday: <input type="text" name="birthday" /></li>
|
||||||
</ul>
|
|
||||||
|
|
||||||
If the "id" attribute is specified in the Form and auto_id is True, the "id"
|
If the "id" attribute is specified in the Form and auto_id is True, the "id"
|
||||||
attribute in the Form gets precedence.
|
attribute in the Form gets precedence.
|
||||||
>>> p = PersonNew(auto_id=True)
|
>>> p = PersonNew(auto_id=True)
|
||||||
>>> print p.as_ul()
|
>>> print p.as_ul()
|
||||||
<ul>
|
|
||||||
<li>First name: <input type="text" id="first_name_id" name="first_name" /></li>
|
<li>First name: <input type="text" id="first_name_id" name="first_name" /></li>
|
||||||
<li>Last name: <input type="text" name="last_name" id="last_name" /></li>
|
<li>Last name: <input type="text" name="last_name" id="last_name" /></li>
|
||||||
<li>Birthday: <input type="text" name="birthday" id="birthday" /></li>
|
<li>Birthday: <input type="text" name="birthday" id="birthday" /></li>
|
||||||
</ul>
|
|
||||||
|
|
||||||
>>> class SignupForm(Form):
|
>>> class SignupForm(Form):
|
||||||
... email = EmailField()
|
... email = EmailField()
|
||||||
|
@ -1158,36 +1136,28 @@ Form.clean() still needs to return a dictionary of all clean data:
|
||||||
... return self.clean_data
|
... return self.clean_data
|
||||||
>>> f = UserRegistration()
|
>>> f = UserRegistration()
|
||||||
>>> print f.as_table()
|
>>> print f.as_table()
|
||||||
<table>
|
|
||||||
<tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
|
<tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
|
||||||
<tr><td>Password1:</td><td><input type="password" name="password1" /></td></tr>
|
<tr><td>Password1:</td><td><input type="password" name="password1" /></td></tr>
|
||||||
<tr><td>Password2:</td><td><input type="password" name="password2" /></td></tr>
|
<tr><td>Password2:</td><td><input type="password" name="password2" /></td></tr>
|
||||||
</table>
|
|
||||||
>>> f.errors()
|
>>> f.errors()
|
||||||
{'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']}
|
{'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']}
|
||||||
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'})
|
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'})
|
||||||
>>> f.errors()
|
>>> f.errors()
|
||||||
{'__all__': [u'Please make sure your passwords match.']}
|
{'__all__': [u'Please make sure your passwords match.']}
|
||||||
>>> print f.as_table()
|
>>> print f.as_table()
|
||||||
<table>
|
|
||||||
<tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr>
|
<tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr>
|
||||||
<tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr>
|
<tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr>
|
||||||
<tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr>
|
<tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr>
|
||||||
</table>
|
|
||||||
>>> print f.as_table_with_errors()
|
>>> print f.as_table_with_errors()
|
||||||
<table>
|
|
||||||
<tr><td colspan="2"><ul><li>Please make sure your passwords match.</li></ul></td></tr>
|
<tr><td colspan="2"><ul><li>Please make sure your passwords match.</li></ul></td></tr>
|
||||||
<tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr>
|
<tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr>
|
||||||
<tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr>
|
<tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr>
|
||||||
<tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr>
|
<tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr>
|
||||||
</table>
|
|
||||||
>>> print f.as_ul_with_errors()
|
>>> print f.as_ul_with_errors()
|
||||||
<ul>
|
|
||||||
<li><ul><li>Please make sure your passwords match.</li></ul></li>
|
<li><ul><li>Please make sure your passwords match.</li></ul></li>
|
||||||
<li>Username: <input type="text" name="username" value="adrian" /></li>
|
<li>Username: <input type="text" name="username" value="adrian" /></li>
|
||||||
<li>Password1: <input type="password" name="password1" value="foo" /></li>
|
<li>Password1: <input type="password" name="password1" value="foo" /></li>
|
||||||
<li>Password2: <input type="password" name="password2" value="bar" /></li>
|
<li>Password2: <input type="password" name="password2" value="bar" /></li>
|
||||||
</ul>
|
|
||||||
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'})
|
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'})
|
||||||
>>> f.errors()
|
>>> f.errors()
|
||||||
{}
|
{}
|
||||||
|
@ -1205,11 +1175,9 @@ subclass' __init__().
|
||||||
... self.fields['birthday'] = DateField()
|
... self.fields['birthday'] = DateField()
|
||||||
>>> p = Person()
|
>>> p = Person()
|
||||||
>>> print p
|
>>> print p
|
||||||
<table>
|
|
||||||
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
|
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
|
||||||
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
|
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
|
||||||
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
|
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
|
||||||
</table>
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue