Ticket #3286: tests2.py.diff

File tests2.py.diff, 10.0 KB (added by Jannis <jl@…>, 18 years ago)

revision of tests and docs for new as_div()

  • tests/regressiontests/forms/tests.py

     
    15751575<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
    15761576<p><ul class="errorlist"><li>This field is required.</li></ul></p>
    15771577<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
     1578>>> print p.as_div()
     1579<div><ul class="errorlist"><li>This field is required.</li></ul></div>
     1580<div><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></div>
     1581<div><ul class="errorlist"><li>This field is required.</li></ul></div>
     1582<div><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></div>
     1583<div><ul class="errorlist"><li>This field is required.</li></ul></div>
     1584<div><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></div>
    15781585
    15791586If you don't pass any values to the Form's __init__(), or if you pass None,
    15801587the Form will be considered unbound and won't do any validation. Form.errors
     
    16061613<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
    16071614<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
    16081615<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
     1616>>> print p.as_div()
     1617<div><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></div>
     1618<div><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></div>
     1619<div><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></div>
    16091620
    16101621Unicode values are handled properly.
    16111622>>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'})
     
    16151626u'<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></li>\n<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></li>\n<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></li>'
    16161627>>> p.as_p()
    16171628u'<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></p>\n<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></p>\n<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></p>'
     1629>>> p.as_div()
     1630u'<div><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></div>\n<div><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></div>\n<div><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></div>'
    16181631
    16191632>>> p = Person({'last_name': u'Lennon'})
    16201633>>> p.errors
     
    16751688<p><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></p>
    16761689<p><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></p>
    16771690<p><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></p>
     1691>>> print p.as_div()
     1692<div><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></div>
     1693<div><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></div>
     1694<div><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></div>
    16781695
    16791696If auto_id is any True value whose str() does not contain '%s', the "id"
    16801697attribute will be the name of the field.
     
    18371854<li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
    18381855<li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
    18391856</ul></p>
     1857>>> print f.as_div()
     1858<div><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></div>
     1859<div><label for="id_language_0">Language:</label> <ul>
     1860<li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
     1861<li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
     1862</ul></div>
    18401863
    18411864MultipleChoiceField is a special case, as its data is required to be a list:
    18421865>>> class SongForm(Form):
     
    20222045<tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>
    20232046<tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>
    20242047
    2025 HiddenInput widgets are displayed differently in the as_table(), as_ul()
    2026 and as_p() output of a Form -- their verbose names are not displayed, and a
    2027 separate row is not displayed. They're displayed in the last row of the
     2048HiddenInput widgets are displayed differently in the as_table(), as_ul(),
     2049as_p() and as_div() output of a Form -- their verbose names are not displayed,
     2050and a separate row is not displayed. They're displayed in the last row of the
    20282051form, directly after that row's form element.
    20292052>>> class Person(Form):
    20302053...     first_name = CharField()
     
    20442067<p>First name: <input type="text" name="first_name" /></p>
    20452068<p>Last name: <input type="text" name="last_name" /></p>
    20462069<p>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p>
     2070>>> print p.as_div()
     2071<div>First name: <input type="text" name="first_name" /></div>
     2072<div>Last name: <input type="text" name="last_name" /></div>
     2073<div>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></div>
    20472074
    20482075With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
    20492076>>> p = Person(auto_id='id_%s')
     
    20592086<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
    20602087<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
    20612088<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></p>
     2089>>> print p.as_div()
     2090<div><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></div>
     2091<div><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></div>
     2092<div><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></div>
    20622093
    20632094If a field with a HiddenInput has errors, the as_table() and as_ul() output
    20642095will include the error message(s) with the text "(Hidden field [fieldname]) "
     
    20802111<p>First name: <input type="text" name="first_name" value="John" /></p>
    20812112<p>Last name: <input type="text" name="last_name" value="Lennon" /></p>
    20822113<p>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>
     2114>>> print p.as_div()
     2115<div><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></div>
     2116<div>First name: <input type="text" name="first_name" value="John" /></div>
     2117<div>Last name: <input type="text" name="last_name" value="Lennon" /></div>
     2118<div>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></div>
    20832119
     2120
    20842121A corner case: It's possible for a form to have only HiddenInputs.
    20852122>>> class TestForm(Form):
    20862123...     foo = CharField(widget=HiddenInput)
     
    20922129<input type="hidden" name="foo" /><input type="hidden" name="bar" />
    20932130>>> print p.as_p()
    20942131<input type="hidden" name="foo" /><input type="hidden" name="bar" />
     2132>>> print p.as_div()
     2133<input type="hidden" name="foo" /><input type="hidden" name="bar" />
    20952134
    20962135A Form's fields are displayed in the same order in which they were defined.
    20972136>>> class TestForm(Form):
  • docs/newforms.txt

     
    376376    <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>
    377377    <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>
    378378
     379``as_div()``
     380~~~~~~~~~~~
     381
     382``Form.as_div()`` renders the form as a series of ``<div>`` tags, with each ``<div>``
     383containing one field::
     384
     385    >>> f = ContactForm()
     386    >>> f.as_div()
     387    u'<div><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></div>\n<div><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></div>\n<div><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></div>\n<div><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></div>'
     388    >>> print f.as_div()
     389    <div><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></div>
     390    <div><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></div>
     391    <div><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></div>
     392    <div><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></div>
     393
    379394``as_table()``
    380395~~~~~~~~~~~~~~
Back to Top