Ticket #3512: html_class4.2.diff
File html_class4.2.diff, 103.1 KB (added by , 17 years ago) |
---|
-
django/newforms/forms.py
110 110 """ 111 111 return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name 112 112 113 def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row ):113 def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row, html_class_list=None): 114 114 "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." 115 115 top_errors = self.non_field_errors() # Errors that should be displayed above all fields. 116 116 output, hidden_fields = [], [] 117 html_class_list = html_class_list or [] 117 118 for name, field in self.fields.items(): 118 119 bf = BoundField(self, field, name) 119 120 bf_errors = ErrorList([escape(error) for error in bf.errors]) # Escape and cache in local variable. … … 122 123 top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors]) 123 124 hidden_fields.append(unicode(bf)) 124 125 else: 126 class_list = list(html_class_list) # A fresh list for each loop. 127 if bf_errors: 128 class_list.append('error') 129 if bf.field.required: 130 class_list.append('required') 131 if class_list: 132 html_class = ' class="%s"' % ' '.join(class_list) 133 else: 134 html_class = '' 125 135 if errors_on_separate_row and bf_errors: 126 136 output.append(error_row % bf_errors) 127 137 if bf.label: … … 136 146 help_text = help_text_html % field.help_text 137 147 else: 138 148 help_text = u'' 139 output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text })149 output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text, 'html_class': html_class}) 140 150 if top_errors: 141 151 output.insert(0, error_row % top_errors) 142 152 if hidden_fields: # Insert any hidden fields in the last row. … … 151 161 152 162 def as_table(self): 153 163 "Returns this form rendered as HTML <tr>s -- excluding the <table></table>." 154 return self._html_output(u'<tr ><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)164 return self._html_output(u'<tr%(html_class)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False) 155 165 156 166 def as_ul(self): 157 167 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>." 158 return self._html_output(u'<li >%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)168 return self._html_output(u'<li%(html_class)s>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False) 159 169 160 170 def as_p(self): 161 171 "Returns this form rendered as HTML <p>s." 162 return self._html_output(u'<p >%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True)172 return self._html_output(u'<p%(html_class)s>%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True) 163 173 164 174 def non_field_errors(self): 165 175 """ -
docs/newforms.txt
328 328 329 329 >>> f = ContactForm() 330 330 >>> print f 331 <tr ><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>332 <tr ><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>333 <tr ><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>334 <tr ><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>331 <tr class="required"><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr> 332 <tr class="required"><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr> 333 <tr class="required"><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> 334 <tr class="required"><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr> 335 335 336 336 If the form is bound to data, the HTML output will include that data 337 337 appropriately. For example, if a field is represented by an … … 345 345 ... 'cc_myself': True} 346 346 >>> f = ContactForm(data) 347 347 >>> print f 348 <tr ><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" value="hello" /></td></tr>349 <tr ><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" value="Hi there" /></td></tr>350 <tr ><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" value="foo@example.com" /></td></tr>351 <tr ><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" checked="checked" /></td></tr>348 <tr class="required"><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" value="hello" /></td></tr> 349 <tr class="required"><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" value="Hi there" /></td></tr> 350 <tr class="required"><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" value="foo@example.com" /></td></tr> 351 <tr class="required"><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" checked="checked" /></td></tr> 352 352 353 353 This default output is a two-column HTML table, with a ``<tr>`` for each field. 354 354 Notice the following: … … 389 389 390 390 >>> f = ContactForm() 391 391 >>> f.as_p() 392 u'<p ><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p>\n<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>\n<p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>\n<p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>'392 u'<p class="required"><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p>\n<p class="required"><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>\n<p class="required"><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>\n<p class="required"><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>' 393 393 >>> print f.as_p() 394 <p ><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p>395 <p ><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>396 <p ><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>397 <p ><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>394 <p class="required"><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p> 395 <p class="required"><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p> 396 <p class="required"><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p> 397 <p class="required"><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p> 398 398 399 399 ``as_ul()`` 400 400 ~~~~~~~~~~~ … … 405 405 406 406 >>> f = ContactForm() 407 407 >>> f.as_ul() 408 u'<li ><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>\n<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>\n<li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>\n<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>'408 u'<li class="required"><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>\n<li class="required"><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>\n<li class="required"><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>\n<li class="required"><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>' 409 409 >>> print f.as_ul() 410 <li ><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>411 <li ><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>412 <li ><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>413 <li ><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>410 <li class="required"><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li> 411 <li class="required"><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li> 412 <li class="required"><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li> 413 <li class="required"><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li> 414 414 415 415 ``as_table()`` 416 416 ~~~~~~~~~~~~~~ … … 421 421 422 422 >>> f = ContactForm() 423 423 >>> f.as_table() 424 u'<tr ><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>\n<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>\n<tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>\n<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>'424 u'<tr class="required"><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>\n<tr class="required"><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>\n<tr class="required"><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>\n<tr class="required"><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>' 425 425 >>> print f.as_table() 426 <tr ><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>427 <tr ><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>428 <tr ><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>429 <tr ><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>426 <tr class="required"><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr> 427 <tr class="required"><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr> 428 <tr class="required"><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> 429 <tr class="required"><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr> 430 430 431 431 Configuring HTML ``<label>`` tags 432 432 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 449 449 450 450 >>> f = ContactForm(auto_id=False) 451 451 >>> print f.as_table() 452 <tr ><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /></td></tr>453 <tr ><th>Message:</th><td><input type="text" name="message" /></td></tr>454 <tr ><th>Sender:</th><td><input type="text" name="sender" /></td></tr>455 <tr ><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr>452 <tr class="required"><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /></td></tr> 453 <tr class="required"><th>Message:</th><td><input type="text" name="message" /></td></tr> 454 <tr class="required"><th>Sender:</th><td><input type="text" name="sender" /></td></tr> 455 <tr class="required"><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr> 456 456 >>> print f.as_ul() 457 <li >Subject: <input type="text" name="subject" maxlength="100" /></li>458 <li >Message: <input type="text" name="message" /></li>459 <li >Sender: <input type="text" name="sender" /></li>460 <li >Cc myself: <input type="checkbox" name="cc_myself" /></li>457 <li class="required">Subject: <input type="text" name="subject" maxlength="100" /></li> 458 <li class="required">Message: <input type="text" name="message" /></li> 459 <li class="required">Sender: <input type="text" name="sender" /></li> 460 <li class="required">Cc myself: <input type="checkbox" name="cc_myself" /></li> 461 461 >>> print f.as_p() 462 <p >Subject: <input type="text" name="subject" maxlength="100" /></p>463 <p >Message: <input type="text" name="message" /></p>464 <p >Sender: <input type="text" name="sender" /></p>465 <p >Cc myself: <input type="checkbox" name="cc_myself" /></p>462 <p class="required">Subject: <input type="text" name="subject" maxlength="100" /></p> 463 <p class="required">Message: <input type="text" name="message" /></p> 464 <p class="required">Sender: <input type="text" name="sender" /></p> 465 <p class="required">Cc myself: <input type="checkbox" name="cc_myself" /></p> 466 466 467 467 If ``auto_id`` is set to ``True``, then the form output *will* include 468 468 ``<label>`` tags and will simply use the field name as its ``id`` for each form … … 470 470 471 471 >>> f = ContactForm(auto_id=True) 472 472 >>> print f.as_table() 473 <tr ><th><label for="subject">Subject:</label></th><td><input id="subject" type="text" name="subject" maxlength="100" /></td></tr>474 <tr ><th><label for="message">Message:</label></th><td><input type="text" name="message" id="message" /></td></tr>475 <tr ><th><label for="sender">Sender:</label></th><td><input type="text" name="sender" id="sender" /></td></tr>476 <tr ><th><label for="cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="cc_myself" /></td></tr>473 <tr class="required"><th><label for="subject">Subject:</label></th><td><input id="subject" type="text" name="subject" maxlength="100" /></td></tr> 474 <tr class="required"><th><label for="message">Message:</label></th><td><input type="text" name="message" id="message" /></td></tr> 475 <tr class="required"><th><label for="sender">Sender:</label></th><td><input type="text" name="sender" id="sender" /></td></tr> 476 <tr class="required"><th><label for="cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="cc_myself" /></td></tr> 477 477 >>> print f.as_ul() 478 <li ><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></li>479 <li ><label for="message">Message:</label> <input type="text" name="message" id="message" /></li>480 <li ><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></li>481 <li ><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></li>478 <li class="required"><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></li> 479 <li class="required"><label for="message">Message:</label> <input type="text" name="message" id="message" /></li> 480 <li class="required"><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></li> 481 <li class="required"><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></li> 482 482 >>> print f.as_p() 483 <p ><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></p>484 <p ><label for="message">Message:</label> <input type="text" name="message" id="message" /></p>485 <p ><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></p>486 <p ><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></p>483 <p class="required"><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></p> 484 <p class="required"><label for="message">Message:</label> <input type="text" name="message" id="message" /></p> 485 <p class="required"><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></p> 486 <p class="required"><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></p> 487 487 488 488 If ``auto_id`` is set to a string containing the format character ``'%s'``, 489 489 then the form output will include ``<label>`` tags, and will generate ``id`` … … 493 493 494 494 >>> f = ContactForm(auto_id='id_for_%s') 495 495 >>> print f.as_table() 496 <tr ><th><label for="id_for_subject">Subject:</label></th><td><input id="id_for_subject" type="text" name="subject" maxlength="100" /></td></tr>497 <tr ><th><label for="id_for_message">Message:</label></th><td><input type="text" name="message" id="id_for_message" /></td></tr>498 <tr ><th><label for="id_for_sender">Sender:</label></th><td><input type="text" name="sender" id="id_for_sender" /></td></tr>499 <tr ><th><label for="id_for_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></td></tr>496 <tr class="required"><th><label for="id_for_subject">Subject:</label></th><td><input id="id_for_subject" type="text" name="subject" maxlength="100" /></td></tr> 497 <tr class="required"><th><label for="id_for_message">Message:</label></th><td><input type="text" name="message" id="id_for_message" /></td></tr> 498 <tr class="required"><th><label for="id_for_sender">Sender:</label></th><td><input type="text" name="sender" id="id_for_sender" /></td></tr> 499 <tr class="required"><th><label for="id_for_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></td></tr> 500 500 >>> print f.as_ul() 501 <li ><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li>502 <li ><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></li>503 <li ><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></li>504 <li ><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li>501 <li class="required"><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li> 502 <li class="required"><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></li> 503 <li class="required"><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></li> 504 <li class="required"><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li> 505 505 >>> print f.as_p() 506 <p ><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></p>507 <p ><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></p>508 <p ><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></p>509 <p ><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></p>506 <p class="required"><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></p> 507 <p class="required"><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></p> 508 <p class="required"><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></p> 509 <p class="required"><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></p> 510 510 511 511 If ``auto_id`` is set to any other true value -- such as a string that doesn't 512 512 include ``%s`` -- then the library will act as if ``auto_id`` is ``True``. … … 537 537 ... 'cc_myself': True} 538 538 >>> f = ContactForm(data, auto_id=False) 539 539 >>> print f.as_table() 540 <tr ><th>Subject:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="subject" maxlength="100" /></td></tr>541 <tr ><th>Message:</th><td><input type="text" name="message" value="Hi there" /></td></tr>542 <tr ><th>Sender:</th><td><ul class="errorlist"><li>Enter a valid e-mail address.</li></ul><input type="text" name="sender" value="invalid e-mail address" /></td></tr>543 <tr ><th>Cc myself:</th><td><input checked="checked" type="checkbox" name="cc_myself" /></td></tr>540 <tr class="error required"><th>Subject:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="subject" maxlength="100" /></td></tr> 541 <tr class="required"><th>Message:</th><td><input type="text" name="message" value="Hi there" /></td></tr> 542 <tr class="error required"><th>Sender:</th><td><ul class="errorlist"><li>Enter a valid e-mail address.</li></ul><input type="text" name="sender" value="invalid e-mail address" /></td></tr> 543 <tr class="required"><th>Cc myself:</th><td><input checked="checked" type="checkbox" name="cc_myself" /></td></tr> 544 544 >>> print f.as_ul() 545 <li ><ul class="errorlist"><li>This field is required.</li></ul>Subject: <input type="text" name="subject" maxlength="100" /></li>546 <li >Message: <input type="text" name="message" value="Hi there" /></li>547 <li ><ul class="errorlist"><li>Enter a valid e-mail address.</li></ul>Sender: <input type="text" name="sender" value="invalid e-mail address" /></li>548 <li >Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></li>545 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Subject: <input type="text" name="subject" maxlength="100" /></li> 546 <li class="required">Message: <input type="text" name="message" value="Hi there" /></li> 547 <li class="error required"><ul class="errorlist"><li>Enter a valid e-mail address.</li></ul>Sender: <input type="text" name="sender" value="invalid e-mail address" /></li> 548 <li class="required">Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></li> 549 549 >>> print f.as_p() 550 550 <p><ul class="errorlist"><li>This field is required.</li></ul></p> 551 <p >Subject: <input type="text" name="subject" maxlength="100" /></p>552 <p >Message: <input type="text" name="message" value="Hi there" /></p>551 <p class="error required">Subject: <input type="text" name="subject" maxlength="100" /></p> 552 <p class="required">Message: <input type="text" name="message" value="Hi there" /></p> 553 553 <p><ul class="errorlist"><li>Enter a valid e-mail address.</li></ul></p> 554 <p >Sender: <input type="text" name="sender" value="invalid e-mail address" /></p>555 <p >Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p>554 <p class="error required">Sender: <input type="text" name="sender" value="invalid e-mail address" /></p> 555 <p class="required">Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p> 556 556 557 You will notice that each row which is required is given the HTML class name 558 ``required`` and any row which the field did not pass validation receives the 559 ``error`` class name. 560 557 561 More granular output 558 562 ~~~~~~~~~~~~~~~~~~~~ 559 563 … … 728 732 ... priority = forms.CharField() 729 733 >>> f = ContactFormWithPriority(auto_id=False) 730 734 >>> print f.as_ul() 731 <li >Subject: <input type="text" name="subject" maxlength="100" /></li>732 <li >Message: <input type="text" name="message" /></li>733 <li >Sender: <input type="text" name="sender" /></li>734 <li >Cc myself: <input type="checkbox" name="cc_myself" /></li>735 <li >Priority: <input type="text" name="priority" /></li>735 <li class="required">Subject: <input type="text" name="subject" maxlength="100" /></li> 736 <li class="required">Message: <input type="text" name="message" /></li> 737 <li class="required">Sender: <input type="text" name="sender" /></li> 738 <li class="required">Cc myself: <input type="checkbox" name="cc_myself" /></li> 739 <li class="required">Priority: <input type="text" name="priority" /></li> 736 740 737 741 It's possible to subclass multiple forms, treating forms as "mix-ins." In this 738 742 example, ``BeatleForm`` subclasses both ``PersonForm`` and ``InstrumentForm`` … … 748 752 ... haircut_type = CharField() 749 753 >>> b = BeatleForm(auto_id=False) 750 754 >>> print b.as_ul() 751 <li >First name: <input type="text" name="first_name" /></li>752 <li >Last name: <input type="text" name="last_name" /></li>753 <li >Instrument: <input type="text" name="instrument" /></li>754 <li >Haircut type: <input type="text" name="haircut_type" /></li>755 <li class="required">First name: <input type="text" name="first_name" /></li> 756 <li class="required">Last name: <input type="text" name="last_name" /></li> 757 <li class="required">Instrument: <input type="text" name="instrument" /></li> 758 <li class="required">Haircut type: <input type="text" name="haircut_type" /></li> 755 759 756 760 Fields 757 761 ====== … … 858 862 ... comment = forms.CharField() 859 863 >>> f = CommentForm(auto_id=False) 860 864 >>> print f 861 <tr ><th>Your name:</th><td><input type="text" name="name" /></td></tr>865 <tr class="required"><th>Your name:</th><td><input type="text" name="name" /></td></tr> 862 866 <tr><th>Your Web site:</th><td><input type="text" name="url" /></td></tr> 863 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>867 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 864 868 865 869 ``initial`` 866 870 ~~~~~~~~~~~ … … 877 881 ... comment = forms.CharField() 878 882 >>> f = CommentForm(auto_id=False) 879 883 >>> print f 880 <tr ><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr>881 <tr ><th>Url:</th><td><input type="text" name="url" value="http://" /></td></tr>882 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>884 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr> 885 <tr class="required"><th>Url:</th><td><input type="text" name="url" value="http://" /></td></tr> 886 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 883 887 884 888 You may be thinking, why not just pass a dictionary of the initial values as 885 889 data when displaying the form? Well, if you do that, you'll trigger validation, … … 892 896 >>> default_data = {'name': 'Your name', 'url': 'http://'} 893 897 >>> f = CommentForm(default_data, auto_id=False) 894 898 >>> print f 895 <tr ><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr>896 <tr ><th>Url:</th><td><ul class="errorlist"><li>Enter a valid URL.</li></ul><input type="text" name="url" value="http://" /></td></tr>897 <tr ><th>Comment:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="comment" /></td></tr>899 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr> 900 <tr class="required"><th>Url:</th><td><ul class="errorlist"><li>Enter a valid URL.</li></ul><input type="text" name="url" value="http://" /></td></tr> 901 <tr class="required"><th>Comment:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="comment" /></td></tr> 898 902 899 903 This is why ``initial`` values are only displayed for unbound forms. For bound 900 904 forms, the HTML output will use the bound data. … … 939 943 ... cc_myself = forms.BooleanField() 940 944 >>> f = HelpTextContactForm(auto_id=False) 941 945 >>> print f.as_table() 942 <tr ><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /><br />100 characters max.</td></tr>943 <tr ><th>Message:</th><td><input type="text" name="message" /></td></tr>944 <tr ><th>Sender:</th><td><input type="text" name="sender" /><br />A valid e-mail address, please.</td></tr>945 <tr ><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr>946 <tr class="required"><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /><br />100 characters max.</td></tr> 947 <tr class="required"><th>Message:</th><td><input type="text" name="message" /></td></tr> 948 <tr class="required"><th>Sender:</th><td><input type="text" name="sender" /><br />A valid e-mail address, please.</td></tr> 949 <tr class="required"><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr> 946 950 >>> print f.as_ul() 947 <li >Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</li>948 <li >Message: <input type="text" name="message" /></li>949 <li >Sender: <input type="text" name="sender" /> A valid e-mail address, please.</li>950 <li >Cc myself: <input type="checkbox" name="cc_myself" /></li>951 <li class="required">Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</li> 952 <li class="required">Message: <input type="text" name="message" /></li> 953 <li class="required">Sender: <input type="text" name="sender" /> A valid e-mail address, please.</li> 954 <li class="required">Cc myself: <input type="checkbox" name="cc_myself" /></li> 951 955 >>> print f.as_p() 952 <p >Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</p>953 <p >Message: <input type="text" name="message" /></p>954 <p >Sender: <input type="text" name="sender" /> A valid e-mail address, please.</p>955 <p >Cc myself: <input type="checkbox" name="cc_myself" /></p>956 <p class="required">Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</p> 957 <p class="required">Message: <input type="text" name="message" /></p> 958 <p class="required">Sender: <input type="text" name="sender" /> A valid e-mail address, please.</p> 959 <p class="required">Cc myself: <input type="checkbox" name="cc_myself" /></p> 956 960 957 961 Dynamic initial values 958 962 ---------------------- … … 973 977 ... comment = forms.CharField() 974 978 >>> f = CommentForm(initial={'name': 'your username'}, auto_id=False) 975 979 >>> print f 976 <tr ><th>Name:</th><td><input type="text" name="name" value="your username" /></td></tr>977 <tr ><th>Url:</th><td><input type="text" name="url" /></td></tr>978 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>980 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="your username" /></td></tr> 981 <tr class="required"><th>Url:</th><td><input type="text" name="url" /></td></tr> 982 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 979 983 >>> f = CommentForm(initial={'name': 'another username'}, auto_id=False) 980 984 >>> print f 981 <tr ><th>Name:</th><td><input type="text" name="name" value="another username" /></td></tr>982 <tr ><th>Url:</th><td><input type="text" name="url" /></td></tr>983 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>985 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="another username" /></td></tr> 986 <tr class="required"><th>Url:</th><td><input type="text" name="url" /></td></tr> 987 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 984 988 985 989 Just like the ``initial`` parameter to ``Field``, these values are only 986 990 displayed for unbound forms, and they're not used as fallback values if a … … 997 1001 ... comment = forms.CharField() 998 1002 >>> f = CommentForm(initial={'name': 'instance'}, auto_id=False) 999 1003 >>> print f 1000 <tr ><th>Name:</th><td><input type="text" name="name" value="instance" /></td></tr>1001 <tr ><th>Url:</th><td><input type="text" name="url" /></td></tr>1002 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>1004 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="instance" /></td></tr> 1005 <tr class="required"><th>Url:</th><td><input type="text" name="url" /></td></tr> 1006 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 1003 1007 1004 1008 Built-in ``Field`` classes 1005 1009 -------------------------- -
tests/regressiontests/forms/regressions.py
9 9 ... f1 = CharField(max_length=10, widget=TextInput(attrs=extra_attrs)) 10 10 ... f2 = CharField(widget=TextInput(attrs=extra_attrs)) 11 11 >>> TestForm(auto_id=False).as_p() 12 u'<p >F1: <input type="text" class="special" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" class="special" name="f2" /></p>'12 u'<p class="required">F1: <input type="text" class="special" name="f1" maxlength="10" /></p>\n<p class="required">F2: <input type="text" class="special" name="f2" /></p>' 13 13 14 14 ####################### 15 15 # Tests for form i18n # … … 21 21 ... username = CharField(max_length=10, label=gettext_lazy('Username')) 22 22 >>> f = SomeForm() 23 23 >>> print f.as_p() 24 <p ><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>24 <p class="required"><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p> 25 25 >>> activate('de') 26 26 >>> print f.as_p() 27 <p ><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>27 <p class="required"><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p> 28 28 >>> deactivate() 29 29 30 30 Unicode decoding problems... … … 33 33 ... somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect()) 34 34 >>> f = SomeForm() 35 35 >>> f.as_p() 36 u'<p ><label for="id_somechoice_0">Somechoice:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="0" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="1" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="2" name="somechoice" /> Nainen</label></li>\n</ul></p>'36 u'<p class="required"><label for="id_somechoice_0">Somechoice:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="0" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="1" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="2" name="somechoice" /> Nainen</label></li>\n</ul></p>' 37 37 38 38 ####################### 39 39 # Miscellaneous Tests # -
tests/regressiontests/forms/tests.py
343 343 ... somechoice = ChoiceField(choices=chain((('', '-'*9),), [(thing['id'], thing['name']) for thing in things])) 344 344 >>> f = SomeForm() 345 345 >>> f.as_table() 346 u'<tr ><th><label for="id_somechoice">Somechoice:</label></th><td><select name="somechoice" id="id_somechoice">\n<option value="" selected="selected">---------</option>\n<option value="1">And Boom</option>\n<option value="2">One More Thing!</option>\n</select></td></tr>'346 u'<tr class="required"><th><label for="id_somechoice">Somechoice:</label></th><td><select name="somechoice" id="id_somechoice">\n<option value="" selected="selected">---------</option>\n<option value="1">And Boom</option>\n<option value="2">One More Thing!</option>\n</select></td></tr>' 347 347 >>> f.as_table() 348 u'<tr ><th><label for="id_somechoice">Somechoice:</label></th><td><select name="somechoice" id="id_somechoice">\n<option value="" selected="selected">---------</option>\n<option value="1">And Boom</option>\n<option value="2">One More Thing!</option>\n</select></td></tr>'348 u'<tr class="required"><th><label for="id_somechoice">Somechoice:</label></th><td><select name="somechoice" id="id_somechoice">\n<option value="" selected="selected">---------</option>\n<option value="1">And Boom</option>\n<option value="2">One More Thing!</option>\n</select></td></tr>' 349 349 >>> f = SomeForm({'somechoice': 2}) 350 350 >>> f.as_table() 351 u'<tr ><th><label for="id_somechoice">Somechoice:</label></th><td><select name="somechoice" id="id_somechoice">\n<option value="">---------</option>\n<option value="1">And Boom</option>\n<option value="2" selected="selected">One More Thing!</option>\n</select></td></tr>'351 u'<tr class="required"><th><label for="id_somechoice">Somechoice:</label></th><td><select name="somechoice" id="id_somechoice">\n<option value="">---------</option>\n<option value="1">And Boom</option>\n<option value="2" selected="selected">One More Thing!</option>\n</select></td></tr>' 352 352 353 353 You can also pass 'choices' to the constructor: 354 354 >>> w = Select(choices=[(1, 1), (2, 2), (3, 3)]) … … 1929 1929 Last name Lennon 1930 1930 Birthday 1940-10-9 1931 1931 >>> print p 1932 <tr ><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>1933 <tr ><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="Lennon" id="id_last_name" /></td></tr>1934 <tr ><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>1932 <tr class="required"><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr> 1933 <tr class="required"><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="Lennon" id="id_last_name" /></td></tr> 1934 <tr class="required"><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr> 1935 1935 1936 1936 Empty dictionaries are valid, too. 1937 1937 >>> p = Person({}) … … 1946 1946 ... 1947 1947 AttributeError: 'Person' object has no attribute 'cleaned_data' 1948 1948 >>> print p 1949 <tr ><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr>1950 <tr ><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr>1951 <tr ><th><label for="id_birthday">Birthday:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="birthday" id="id_birthday" /></td></tr>1949 <tr class="error required"><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr> 1950 <tr class="error required"><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr> 1951 <tr class="error required"><th><label for="id_birthday">Birthday:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="birthday" id="id_birthday" /></td></tr> 1952 1952 >>> print p.as_table() 1953 <tr ><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr>1954 <tr ><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr>1955 <tr ><th><label for="id_birthday">Birthday:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="birthday" id="id_birthday" /></td></tr>1953 <tr class="error required"><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr> 1954 <tr class="error required"><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr> 1955 <tr class="error required"><th><label for="id_birthday">Birthday:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="birthday" id="id_birthday" /></td></tr> 1956 1956 >>> print p.as_ul() 1957 <li ><ul class="errorlist"><li>This field is required.</li></ul><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>1958 <li ><ul class="errorlist"><li>This field is required.</li></ul><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>1959 <li ><ul class="errorlist"><li>This field is required.</li></ul><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>1957 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li> 1958 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li> 1959 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li> 1960 1960 >>> print p.as_p() 1961 1961 <p><ul class="errorlist"><li>This field is required.</li></ul></p> 1962 <p ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>1962 <p class="error required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p> 1963 1963 <p><ul class="errorlist"><li>This field is required.</li></ul></p> 1964 <p ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>1964 <p class="error required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p> 1965 1965 <p><ul class="errorlist"><li>This field is required.</li></ul></p> 1966 <p ><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>1966 <p class="error required"><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p> 1967 1967 1968 1968 If you don't pass any values to the Form's __init__(), or if you pass None, 1969 1969 the Form will be considered unbound and won't do any validation. Form.errors … … 1980 1980 ... 1981 1981 AttributeError: 'Person' object has no attribute 'cleaned_data' 1982 1982 >>> print p 1983 <tr ><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>1984 <tr ><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>1985 <tr ><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr>1983 <tr class="required"><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr> 1984 <tr class="required"><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr> 1985 <tr class="required"><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr> 1986 1986 >>> print p.as_table() 1987 <tr ><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>1988 <tr ><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>1989 <tr ><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr>1987 <tr class="required"><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr> 1988 <tr class="required"><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr> 1989 <tr class="required"><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr> 1990 1990 >>> print p.as_ul() 1991 <li ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>1992 <li ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>1993 <li ><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>1991 <li class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li> 1992 <li class="required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li> 1993 <li class="required"><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li> 1994 1994 >>> print p.as_p() 1995 <p ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>1996 <p ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>1997 <p ><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>1995 <p class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p> 1996 <p class="required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p> 1997 <p class="required"><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p> 1998 1998 1999 1999 Unicode values are handled properly. 2000 2000 >>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'}) 2001 2001 >>> p.as_table() 2002 u'<tr ><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>\n<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></td></tr>\n<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>'2002 u'<tr class="required"><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>\n<tr class="required"><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></td></tr>\n<tr class="required"><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>' 2003 2003 >>> p.as_ul() 2004 u'<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>'2004 u'<li class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></li>\n<li class="required"><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 class="required"><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></li>' 2005 2005 >>> p.as_p() 2006 u'<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>'2006 u'<p class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></p>\n<p class="required"><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 class="required"><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></p>' 2007 2007 2008 2008 >>> p = Person({'last_name': u'Lennon'}) 2009 2009 >>> p.errors … … 2081 2081 the human-readable labels for a field. 2082 2082 >>> p = Person(auto_id='%s_id') 2083 2083 >>> print p.as_table() 2084 <tr ><th><label for="first_name_id">First name:</label></th><td><input type="text" name="first_name" id="first_name_id" /></td></tr>2085 <tr ><th><label for="last_name_id">Last name:</label></th><td><input type="text" name="last_name" id="last_name_id" /></td></tr>2086 <tr ><th><label for="birthday_id">Birthday:</label></th><td><input type="text" name="birthday" id="birthday_id" /></td></tr>2084 <tr class="required"><th><label for="first_name_id">First name:</label></th><td><input type="text" name="first_name" id="first_name_id" /></td></tr> 2085 <tr class="required"><th><label for="last_name_id">Last name:</label></th><td><input type="text" name="last_name" id="last_name_id" /></td></tr> 2086 <tr class="required"><th><label for="birthday_id">Birthday:</label></th><td><input type="text" name="birthday" id="birthday_id" /></td></tr> 2087 2087 >>> print p.as_ul() 2088 <li ><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></li>2089 <li ><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></li>2090 <li ><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></li>2088 <li class="required"><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></li> 2089 <li class="required"><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></li> 2090 <li class="required"><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></li> 2091 2091 >>> print p.as_p() 2092 <p ><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></p>2093 <p ><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></p>2094 <p ><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></p>2092 <p class="required"><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></p> 2093 <p class="required"><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></p> 2094 <p class="required"><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></p> 2095 2095 2096 2096 If auto_id is any True value whose str() does not contain '%s', the "id" 2097 2097 attribute will be the name of the field. 2098 2098 >>> p = Person(auto_id=True) 2099 2099 >>> print p.as_ul() 2100 <li ><label for="first_name">First name:</label> <input type="text" name="first_name" id="first_name" /></li>2101 <li ><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li>2102 <li ><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li>2100 <li class="required"><label for="first_name">First name:</label> <input type="text" name="first_name" id="first_name" /></li> 2101 <li class="required"><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li> 2102 <li class="required"><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li> 2103 2103 2104 2104 If auto_id is any False value, an "id" attribute won't be output unless it 2105 2105 was manually entered. 2106 2106 >>> p = Person(auto_id=False) 2107 2107 >>> print p.as_ul() 2108 <li >First name: <input type="text" name="first_name" /></li>2109 <li >Last name: <input type="text" name="last_name" /></li>2110 <li >Birthday: <input type="text" name="birthday" /></li>2108 <li class="required">First name: <input type="text" name="first_name" /></li> 2109 <li class="required">Last name: <input type="text" name="last_name" /></li> 2110 <li class="required">Birthday: <input type="text" name="birthday" /></li> 2111 2111 2112 2112 In this example, auto_id is False, but the "id" attribute for the "first_name" 2113 2113 field is given. Also note that field gets a <label>, while the others don't. … … 2117 2117 ... birthday = DateField() 2118 2118 >>> p = PersonNew(auto_id=False) 2119 2119 >>> print p.as_ul() 2120 <li ><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li>2121 <li >Last name: <input type="text" name="last_name" /></li>2122 <li >Birthday: <input type="text" name="birthday" /></li>2120 <li class="required"><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li> 2121 <li class="required">Last name: <input type="text" name="last_name" /></li> 2122 <li class="required">Birthday: <input type="text" name="birthday" /></li> 2123 2123 2124 2124 If the "id" attribute is specified in the Form and auto_id is True, the "id" 2125 2125 attribute in the Form gets precedence. 2126 2126 >>> p = PersonNew(auto_id=True) 2127 2127 >>> print p.as_ul() 2128 <li ><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li>2129 <li ><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li>2130 <li ><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li>2128 <li class="required"><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li> 2129 <li class="required"><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li> 2130 <li class="required"><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li> 2131 2131 2132 2132 >>> class SignupForm(Form): 2133 2133 ... email = EmailField() … … 2275 2275 <li><label><input type="radio" name="language" value="J" /> Java</label></li> 2276 2276 </ul> 2277 2277 >>> print f 2278 <tr ><th>Name:</th><td><input type="text" name="name" /></td></tr>2279 <tr ><th>Language:</th><td><ul>2278 <tr class="required"><th>Name:</th><td><input type="text" name="name" /></td></tr> 2279 <tr class="required"><th>Language:</th><td><ul> 2280 2280 <li><label><input type="radio" name="language" value="P" /> Python</label></li> 2281 2281 <li><label><input type="radio" name="language" value="J" /> Java</label></li> 2282 2282 </ul></td></tr> 2283 2283 >>> print f.as_ul() 2284 <li >Name: <input type="text" name="name" /></li>2285 <li >Language: <ul>2284 <li class="required">Name: <input type="text" name="name" /></li> 2285 <li class="required">Language: <ul> 2286 2286 <li><label><input type="radio" name="language" value="P" /> Python</label></li> 2287 2287 <li><label><input type="radio" name="language" value="J" /> Java</label></li> 2288 2288 </ul></li> … … 2301 2301 either as_table() or as_ul(), the label for the RadioSelect will point to the 2302 2302 ID of the *first* radio button. 2303 2303 >>> print f 2304 <tr ><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>2305 <tr ><th><label for="id_language_0">Language:</label></th><td><ul>2304 <tr class="required"><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr> 2305 <tr class="required"><th><label for="id_language_0">Language:</label></th><td><ul> 2306 2306 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li> 2307 2307 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li> 2308 2308 </ul></td></tr> 2309 2309 >>> print f.as_ul() 2310 <li ><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>2311 <li ><label for="id_language_0">Language:</label> <ul>2310 <li class="required"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li> 2311 <li class="required"><label for="id_language_0">Language:</label> <ul> 2312 2312 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li> 2313 2313 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li> 2314 2314 </ul></li> 2315 2315 >>> print f.as_p() 2316 <p ><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>2317 <p ><label for="id_language_0">Language:</label> <ul>2316 <p class="required"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p> 2317 <p class="required"><label for="id_language_0">Language:</label> <ul> 2318 2318 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li> 2319 2319 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li> 2320 2320 </ul></p> … … 2412 2412 ... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=MultipleHiddenInput) 2413 2413 >>> f = SongFormHidden(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])), auto_id=False) 2414 2414 >>> print f.as_ul() 2415 <li >Name: <input type="text" name="name" value="Yesterday" /><input type="hidden" name="composers" value="J" />2415 <li class="required">Name: <input type="text" name="name" value="Yesterday" /><input type="hidden" name="composers" value="J" /> 2416 2416 <input type="hidden" name="composers" value="P" /></li> 2417 2417 2418 2418 When using CheckboxSelectMultiple, the framework expects a list of input and … … 2439 2439 2440 2440 >>> f = EscapingForm({'special_name': "Nothing to escape"}, auto_id=False) 2441 2441 >>> print f 2442 <tr ><th>Special name:</th><td><ul class="errorlist"><li>Something's wrong with 'Nothing to escape'</li></ul><input type="text" name="special_name" value="Nothing to escape" /></td></tr>2442 <tr class="error required"><th>Special name:</th><td><ul class="errorlist"><li>Something's wrong with 'Nothing to escape'</li></ul><input type="text" name="special_name" value="Nothing to escape" /></td></tr> 2443 2443 >>> f = EscapingForm({'special_name': "Should escape < & > and <script>alert('xss')</script>"}, auto_id=False) 2444 2444 >>> print f 2445 <tr ><th>Special name:</th><td><ul class="errorlist"><li>Something's wrong with 'Should escape < & > and <script>alert('xss')</script>'</li></ul><input type="text" name="special_name" value="Should escape < & > and <script>alert('xss')</script>" /></td></tr>2445 <tr class="error required"><th>Special name:</th><td><ul class="errorlist"><li>Something's wrong with 'Should escape < & > and <script>alert('xss')</script>'</li></ul><input type="text" name="special_name" value="Should escape < & > and <script>alert('xss')</script>" /></td></tr> 2446 2446 2447 2447 # Validating multiple fields in relation to another ########################### 2448 2448 … … 2496 2496 {} 2497 2497 >>> f = UserRegistration({}, auto_id=False) 2498 2498 >>> print f.as_table() 2499 <tr ><th>Username:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="username" maxlength="10" /></td></tr>2500 <tr ><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr>2501 <tr ><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr>2499 <tr class="error required"><th>Username:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="username" maxlength="10" /></td></tr> 2500 <tr class="error required"><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr> 2501 <tr class="error required"><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr> 2502 2502 >>> f.errors 2503 2503 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} 2504 2504 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) … … 2506 2506 {'__all__': [u'Please make sure your passwords match.']} 2507 2507 >>> print f.as_table() 2508 2508 <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr> 2509 <tr ><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr>2510 <tr ><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr>2511 <tr ><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr>2509 <tr class="required"><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr> 2510 <tr class="required"><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr> 2511 <tr class="required"><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr> 2512 2512 >>> print f.as_ul() 2513 2513 <li><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></li> 2514 <li >Username: <input type="text" name="username" value="adrian" maxlength="10" /></li>2515 <li >Password1: <input type="password" name="password1" value="foo" /></li>2516 <li >Password2: <input type="password" name="password2" value="bar" /></li>2514 <li class="required">Username: <input type="text" name="username" value="adrian" maxlength="10" /></li> 2515 <li class="required">Password1: <input type="password" name="password1" value="foo" /></li> 2516 <li class="required">Password2: <input type="password" name="password2" value="bar" /></li> 2517 2517 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False) 2518 2518 >>> f.errors 2519 2519 {} … … 2533 2533 ... self.fields['birthday'] = DateField() 2534 2534 >>> p = Person(auto_id=False) 2535 2535 >>> print p 2536 <tr ><th>First name:</th><td><input type="text" name="first_name" /></td></tr>2537 <tr ><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>2538 <tr ><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>2536 <tr class="required"><th>First name:</th><td><input type="text" name="first_name" /></td></tr> 2537 <tr class="required"><th>Last name:</th><td><input type="text" name="last_name" /></td></tr> 2538 <tr class="required"><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr> 2539 2539 2540 2540 Instances of a dynamic Form do not persist fields from one Form instance to 2541 2541 the next. … … 2547 2547 >>> field_list = [('field1', CharField()), ('field2', CharField())] 2548 2548 >>> my_form = MyForm(field_list=field_list) 2549 2549 >>> print my_form 2550 <tr ><th>Field1:</th><td><input type="text" name="field1" /></td></tr>2551 <tr ><th>Field2:</th><td><input type="text" name="field2" /></td></tr>2550 <tr class="required"><th>Field1:</th><td><input type="text" name="field1" /></td></tr> 2551 <tr class="required"><th>Field2:</th><td><input type="text" name="field2" /></td></tr> 2552 2552 >>> field_list = [('field3', CharField()), ('field4', CharField())] 2553 2553 >>> my_form = MyForm(field_list=field_list) 2554 2554 >>> print my_form 2555 <tr ><th>Field3:</th><td><input type="text" name="field3" /></td></tr>2556 <tr ><th>Field4:</th><td><input type="text" name="field4" /></td></tr>2555 <tr class="required"><th>Field3:</th><td><input type="text" name="field3" /></td></tr> 2556 <tr class="required"><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 2557 2557 2558 2558 >>> class MyForm(Form): 2559 2559 ... default_field_1 = CharField() … … 2565 2565 >>> field_list = [('field1', CharField()), ('field2', CharField())] 2566 2566 >>> my_form = MyForm(field_list=field_list) 2567 2567 >>> print my_form 2568 <tr ><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr>2569 <tr ><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr>2570 <tr ><th>Field1:</th><td><input type="text" name="field1" /></td></tr>2571 <tr ><th>Field2:</th><td><input type="text" name="field2" /></td></tr>2568 <tr class="required"><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr> 2569 <tr class="required"><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr> 2570 <tr class="required"><th>Field1:</th><td><input type="text" name="field1" /></td></tr> 2571 <tr class="required"><th>Field2:</th><td><input type="text" name="field2" /></td></tr> 2572 2572 >>> field_list = [('field3', CharField()), ('field4', CharField())] 2573 2573 >>> my_form = MyForm(field_list=field_list) 2574 2574 >>> print my_form 2575 <tr ><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr>2576 <tr ><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr>2577 <tr ><th>Field3:</th><td><input type="text" name="field3" /></td></tr>2578 <tr ><th>Field4:</th><td><input type="text" name="field4" /></td></tr>2575 <tr class="required"><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr> 2576 <tr class="required"><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr> 2577 <tr class="required"><th>Field3:</th><td><input type="text" name="field3" /></td></tr> 2578 <tr class="required"><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 2579 2579 2580 2580 Similarly, changes to field attributes do not persist from one Form instance 2581 2581 to the next. … … 2625 2625 ... birthday = DateField() 2626 2626 >>> p = Person(auto_id=False) 2627 2627 >>> print p 2628 <tr ><th>First name:</th><td><input type="text" name="first_name" /></td></tr>2629 <tr ><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>2630 <tr ><th>Birthday:</th><td><input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></td></tr>2628 <tr class="required"><th>First name:</th><td><input type="text" name="first_name" /></td></tr> 2629 <tr class="required"><th>Last name:</th><td><input type="text" name="last_name" /></td></tr> 2630 <tr class="required"><th>Birthday:</th><td><input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></td></tr> 2631 2631 >>> print p.as_ul() 2632 <li >First name: <input type="text" name="first_name" /></li>2633 <li >Last name: <input type="text" name="last_name" /></li>2634 <li >Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></li>2632 <li class="required">First name: <input type="text" name="first_name" /></li> 2633 <li class="required">Last name: <input type="text" name="last_name" /></li> 2634 <li class="required">Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></li> 2635 2635 >>> print p.as_p() 2636 <p >First name: <input type="text" name="first_name" /></p>2637 <p >Last name: <input type="text" name="last_name" /></p>2638 <p >Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p>2636 <p class="required">First name: <input type="text" name="first_name" /></p> 2637 <p class="required">Last name: <input type="text" name="last_name" /></p> 2638 <p class="required">Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p> 2639 2639 2640 2640 With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label. 2641 2641 >>> p = Person(auto_id='id_%s') 2642 2642 >>> print p 2643 <tr ><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>2644 <tr ><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>2645 <tr ><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></td></tr>2643 <tr class="required"><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr> 2644 <tr class="required"><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr> 2645 <tr class="required"><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></td></tr> 2646 2646 >>> print p.as_ul() 2647 <li ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>2648 <li ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>2649 <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>2647 <li class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li> 2648 <li class="required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li> 2649 <li class="required"><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> 2650 2650 >>> print p.as_p() 2651 <p ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>2652 <p ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>2653 <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>2651 <p class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p> 2652 <p class="required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p> 2653 <p class="required"><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> 2654 2654 2655 2655 If a field with a HiddenInput has errors, the as_table() and as_ul() output 2656 2656 will include the error message(s) with the text "(Hidden field [fieldname]) " … … 2659 2659 >>> p = Person({'first_name': 'John', 'last_name': 'Lennon', 'birthday': '1940-10-9'}, auto_id=False) 2660 2660 >>> print p 2661 2661 <tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr> 2662 <tr ><th>First name:</th><td><input type="text" name="first_name" value="John" /></td></tr>2663 <tr ><th>Last name:</th><td><input type="text" name="last_name" value="Lennon" /></td></tr>2664 <tr ><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></td></tr>2662 <tr class="required"><th>First name:</th><td><input type="text" name="first_name" value="John" /></td></tr> 2663 <tr class="required"><th>Last name:</th><td><input type="text" name="last_name" value="Lennon" /></td></tr> 2664 <tr class="required"><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></td></tr> 2665 2665 >>> print p.as_ul() 2666 2666 <li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li> 2667 <li >First name: <input type="text" name="first_name" value="John" /></li>2668 <li >Last name: <input type="text" name="last_name" value="Lennon" /></li>2669 <li >Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li>2667 <li class="required">First name: <input type="text" name="first_name" value="John" /></li> 2668 <li class="required">Last name: <input type="text" name="last_name" value="Lennon" /></li> 2669 <li class="required">Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li> 2670 2670 >>> print p.as_p() 2671 2671 <p><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></p> 2672 <p >First name: <input type="text" name="first_name" value="John" /></p>2673 <p >Last name: <input type="text" name="last_name" value="Lennon" /></p>2674 <p >Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>2672 <p class="required">First name: <input type="text" name="first_name" value="John" /></p> 2673 <p class="required">Last name: <input type="text" name="last_name" value="Lennon" /></p> 2674 <p class="required">Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p> 2675 2675 2676 2676 A corner case: It's possible for a form to have only HiddenInputs. 2677 2677 >>> class TestForm(Form): … … 2703 2703 ... field14 = CharField() 2704 2704 >>> p = TestForm(auto_id=False) 2705 2705 >>> print p 2706 <tr ><th>Field1:</th><td><input type="text" name="field1" /></td></tr>2707 <tr ><th>Field2:</th><td><input type="text" name="field2" /></td></tr>2708 <tr ><th>Field3:</th><td><input type="text" name="field3" /></td></tr>2709 <tr ><th>Field4:</th><td><input type="text" name="field4" /></td></tr>2710 <tr ><th>Field5:</th><td><input type="text" name="field5" /></td></tr>2711 <tr ><th>Field6:</th><td><input type="text" name="field6" /></td></tr>2712 <tr ><th>Field7:</th><td><input type="text" name="field7" /></td></tr>2713 <tr ><th>Field8:</th><td><input type="text" name="field8" /></td></tr>2714 <tr ><th>Field9:</th><td><input type="text" name="field9" /></td></tr>2715 <tr ><th>Field10:</th><td><input type="text" name="field10" /></td></tr>2716 <tr ><th>Field11:</th><td><input type="text" name="field11" /></td></tr>2717 <tr ><th>Field12:</th><td><input type="text" name="field12" /></td></tr>2718 <tr ><th>Field13:</th><td><input type="text" name="field13" /></td></tr>2719 <tr ><th>Field14:</th><td><input type="text" name="field14" /></td></tr>2706 <tr class="required"><th>Field1:</th><td><input type="text" name="field1" /></td></tr> 2707 <tr class="required"><th>Field2:</th><td><input type="text" name="field2" /></td></tr> 2708 <tr class="required"><th>Field3:</th><td><input type="text" name="field3" /></td></tr> 2709 <tr class="required"><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 2710 <tr class="required"><th>Field5:</th><td><input type="text" name="field5" /></td></tr> 2711 <tr class="required"><th>Field6:</th><td><input type="text" name="field6" /></td></tr> 2712 <tr class="required"><th>Field7:</th><td><input type="text" name="field7" /></td></tr> 2713 <tr class="required"><th>Field8:</th><td><input type="text" name="field8" /></td></tr> 2714 <tr class="required"><th>Field9:</th><td><input type="text" name="field9" /></td></tr> 2715 <tr class="required"><th>Field10:</th><td><input type="text" name="field10" /></td></tr> 2716 <tr class="required"><th>Field11:</th><td><input type="text" name="field11" /></td></tr> 2717 <tr class="required"><th>Field12:</th><td><input type="text" name="field12" /></td></tr> 2718 <tr class="required"><th>Field13:</th><td><input type="text" name="field13" /></td></tr> 2719 <tr class="required"><th>Field14:</th><td><input type="text" name="field14" /></td></tr> 2720 2720 2721 2721 Some Field classes have an effect on the HTML attributes of their associated 2722 2722 Widget. If you set max_length in a CharField and its associated widget is … … 2729 2729 ... address = CharField() # no max_length defined here 2730 2730 >>> p = UserRegistration(auto_id=False) 2731 2731 >>> print p.as_ul() 2732 <li >Username: <input type="text" name="username" maxlength="10" /></li>2733 <li >Password: <input type="password" name="password" maxlength="10" /></li>2734 <li >Realname: <input type="text" name="realname" maxlength="10" /></li>2735 <li >Address: <input type="text" name="address" /></li>2732 <li class="required">Username: <input type="text" name="username" maxlength="10" /></li> 2733 <li class="required">Password: <input type="password" name="password" maxlength="10" /></li> 2734 <li class="required">Realname: <input type="text" name="realname" maxlength="10" /></li> 2735 <li class="required">Address: <input type="text" name="address" /></li> 2736 2736 2737 2737 If you specify a custom "attrs" that includes the "maxlength" attribute, 2738 2738 the Field's max_length attribute will override whatever "maxlength" you specify … … 2742 2742 ... password = CharField(max_length=10, widget=PasswordInput) 2743 2743 >>> p = UserRegistration(auto_id=False) 2744 2744 >>> print p.as_ul() 2745 <li >Username: <input type="text" name="username" maxlength="10" /></li>2746 <li >Password: <input type="password" name="password" maxlength="10" /></li>2745 <li class="required">Username: <input type="text" name="username" maxlength="10" /></li> 2746 <li class="required">Password: <input type="password" name="password" maxlength="10" /></li> 2747 2747 2748 2748 # Specifying labels ########################################################### 2749 2749 … … 2756 2756 ... password2 = CharField(widget=PasswordInput, label='Password (again)') 2757 2757 >>> p = UserRegistration(auto_id=False) 2758 2758 >>> print p.as_ul() 2759 <li >Your username: <input type="text" name="username" maxlength="10" /></li>2760 <li >Password1: <input type="password" name="password1" /></li>2761 <li >Password (again): <input type="password" name="password2" /></li>2759 <li class="required">Your username: <input type="text" name="username" maxlength="10" /></li> 2760 <li class="required">Password1: <input type="password" name="password1" /></li> 2761 <li class="required">Password (again): <input type="password" name="password2" /></li> 2762 2762 2763 2763 Labels for as_* methods will only end in a colon if they don't end in other 2764 2764 punctuation already. … … 2769 2769 ... q4 = CharField(label='Answer this question!') 2770 2770 ... q5 = CharField(label='The last question. Period.') 2771 2771 >>> print Questions(auto_id=False).as_p() 2772 <p >The first question: <input type="text" name="q1" /></p>2773 <p >What is your name? <input type="text" name="q2" /></p>2774 <p >The answer to life is: <input type="text" name="q3" /></p>2775 <p >Answer this question! <input type="text" name="q4" /></p>2776 <p >The last question. Period. <input type="text" name="q5" /></p>2772 <p class="required">The first question: <input type="text" name="q1" /></p> 2773 <p class="required">What is your name? <input type="text" name="q2" /></p> 2774 <p class="required">The answer to life is: <input type="text" name="q3" /></p> 2775 <p class="required">Answer this question! <input type="text" name="q4" /></p> 2776 <p class="required">The last question. Period. <input type="text" name="q5" /></p> 2777 2777 >>> print Questions().as_p() 2778 <p ><label for="id_q1">The first question:</label> <input type="text" name="q1" id="id_q1" /></p>2779 <p ><label for="id_q2">What is your name?</label> <input type="text" name="q2" id="id_q2" /></p>2780 <p ><label for="id_q3">The answer to life is:</label> <input type="text" name="q3" id="id_q3" /></p>2781 <p ><label for="id_q4">Answer this question!</label> <input type="text" name="q4" id="id_q4" /></p>2782 <p ><label for="id_q5">The last question. Period.</label> <input type="text" name="q5" id="id_q5" /></p>2778 <p class="required"><label for="id_q1">The first question:</label> <input type="text" name="q1" id="id_q1" /></p> 2779 <p class="required"><label for="id_q2">What is your name?</label> <input type="text" name="q2" id="id_q2" /></p> 2780 <p class="required"><label for="id_q3">The answer to life is:</label> <input type="text" name="q3" id="id_q3" /></p> 2781 <p class="required"><label for="id_q4">Answer this question!</label> <input type="text" name="q4" id="id_q4" /></p> 2782 <p class="required"><label for="id_q5">The last question. Period.</label> <input type="text" name="q5" id="id_q5" /></p> 2783 2783 2784 2784 A label can be a Unicode object or a bytestring with special characters. 2785 2785 >>> class UserRegistration(Form): … … 2787 2787 ... password = CharField(widget=PasswordInput, label=u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') 2788 2788 >>> p = UserRegistration(auto_id=False) 2789 2789 >>> p.as_ul() 2790 u'<li >\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111: <input type="text" name="username" maxlength="10" /></li>\n<li>\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111: <input type="password" name="password" /></li>'2790 u'<li class="required">\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111: <input type="text" name="username" maxlength="10" /></li>\n<li class="required">\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111: <input type="password" name="password" /></li>' 2791 2791 2792 2792 If a label is set to the empty string for a field, that field won't get a label. 2793 2793 >>> class UserRegistration(Form): … … 2795 2795 ... password = CharField(widget=PasswordInput) 2796 2796 >>> p = UserRegistration(auto_id=False) 2797 2797 >>> print p.as_ul() 2798 <li > <input type="text" name="username" maxlength="10" /></li>2799 <li >Password: <input type="password" name="password" /></li>2798 <li class="required"> <input type="text" name="username" maxlength="10" /></li> 2799 <li class="required">Password: <input type="password" name="password" /></li> 2800 2800 >>> p = UserRegistration(auto_id='id_%s') 2801 2801 >>> print p.as_ul() 2802 <li > <input id="id_username" type="text" name="username" maxlength="10" /></li>2803 <li ><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li>2802 <li class="required"> <input id="id_username" type="text" name="username" maxlength="10" /></li> 2803 <li class="required"><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> 2804 2804 2805 2805 If label is None, Django will auto-create the label from the field name. This 2806 2806 is default behavior. … … 2809 2809 ... password = CharField(widget=PasswordInput) 2810 2810 >>> p = UserRegistration(auto_id=False) 2811 2811 >>> print p.as_ul() 2812 <li >Username: <input type="text" name="username" maxlength="10" /></li>2813 <li >Password: <input type="password" name="password" /></li>2812 <li class="required">Username: <input type="text" name="username" maxlength="10" /></li> 2813 <li class="required">Password: <input type="password" name="password" /></li> 2814 2814 >>> p = UserRegistration(auto_id='id_%s') 2815 2815 >>> print p.as_ul() 2816 <li ><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li>2817 <li ><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li>2816 <li class="required"><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li> 2817 <li class="required"><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> 2818 2818 2819 2819 # Initial data ################################################################ 2820 2820 … … 2830 2830 Here, we're not submitting any data, so the initial value will be displayed. 2831 2831 >>> p = UserRegistration(auto_id=False) 2832 2832 >>> print p.as_ul() 2833 <li >Username: <input type="text" name="username" value="django" maxlength="10" /></li>2834 <li >Password: <input type="password" name="password" /></li>2833 <li class="required">Username: <input type="text" name="username" value="django" maxlength="10" /></li> 2834 <li class="required">Password: <input type="password" name="password" /></li> 2835 2835 2836 2836 Here, we're submitting data, so the initial value will *not* be displayed. 2837 2837 >>> p = UserRegistration({}, auto_id=False) 2838 2838 >>> print p.as_ul() 2839 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2840 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2839 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2840 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2841 2841 >>> p = UserRegistration({'username': u''}, auto_id=False) 2842 2842 >>> print p.as_ul() 2843 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2844 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2843 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2844 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2845 2845 >>> p = UserRegistration({'username': u'foo'}, auto_id=False) 2846 2846 >>> print p.as_ul() 2847 <li >Username: <input type="text" name="username" value="foo" maxlength="10" /></li>2848 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2847 <li class="required">Username: <input type="text" name="username" value="foo" maxlength="10" /></li> 2848 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2849 2849 2850 2850 An 'initial' value is *not* used as a fallback if data is not provided. In this 2851 2851 example, we don't provide a value for 'username', and the form raises a … … 2871 2871 Here, we're not submitting any data, so the initial value will be displayed. 2872 2872 >>> p = UserRegistration(initial={'username': 'django'}, auto_id=False) 2873 2873 >>> print p.as_ul() 2874 <li >Username: <input type="text" name="username" value="django" maxlength="10" /></li>2875 <li >Password: <input type="password" name="password" /></li>2874 <li class="required">Username: <input type="text" name="username" value="django" maxlength="10" /></li> 2875 <li class="required">Password: <input type="password" name="password" /></li> 2876 2876 >>> p = UserRegistration(initial={'username': 'stephane'}, auto_id=False) 2877 2877 >>> print p.as_ul() 2878 <li >Username: <input type="text" name="username" value="stephane" maxlength="10" /></li>2879 <li >Password: <input type="password" name="password" /></li>2878 <li class="required">Username: <input type="text" name="username" value="stephane" maxlength="10" /></li> 2879 <li class="required">Password: <input type="password" name="password" /></li> 2880 2880 2881 2881 The 'initial' parameter is meaningless if you pass data. 2882 2882 >>> p = UserRegistration({}, initial={'username': 'django'}, auto_id=False) 2883 2883 >>> print p.as_ul() 2884 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2885 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2884 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2885 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2886 2886 >>> p = UserRegistration({'username': u''}, initial={'username': 'django'}, auto_id=False) 2887 2887 >>> print p.as_ul() 2888 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2889 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2888 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2889 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2890 2890 >>> p = UserRegistration({'username': u'foo'}, initial={'username': 'django'}, auto_id=False) 2891 2891 >>> print p.as_ul() 2892 <li >Username: <input type="text" name="username" value="foo" maxlength="10" /></li>2893 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2892 <li class="required">Username: <input type="text" name="username" value="foo" maxlength="10" /></li> 2893 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2894 2894 2895 2895 A dynamic 'initial' value is *not* used as a fallback if data is not provided. 2896 2896 In this example, we don't provide a value for 'username', and the form raises a … … 2908 2908 ... password = CharField(widget=PasswordInput) 2909 2909 >>> p = UserRegistration(initial={'username': 'babik'}, auto_id=False) 2910 2910 >>> print p.as_ul() 2911 <li >Username: <input type="text" name="username" value="babik" maxlength="10" /></li>2912 <li >Password: <input type="password" name="password" /></li>2911 <li class="required">Username: <input type="text" name="username" value="babik" maxlength="10" /></li> 2912 <li class="required">Password: <input type="password" name="password" /></li> 2913 2913 2914 2914 # Callable initial data ######################################################## 2915 2915 … … 2929 2929 Here, we're not submitting any data, so the initial value will be displayed. 2930 2930 >>> p = UserRegistration(initial={'username': initial_django}, auto_id=False) 2931 2931 >>> print p.as_ul() 2932 <li >Username: <input type="text" name="username" value="django" maxlength="10" /></li>2933 <li >Password: <input type="password" name="password" /></li>2932 <li class="required">Username: <input type="text" name="username" value="django" maxlength="10" /></li> 2933 <li class="required">Password: <input type="password" name="password" /></li> 2934 2934 2935 2935 The 'initial' parameter is meaningless if you pass data. 2936 2936 >>> p = UserRegistration({}, initial={'username': initial_django}, auto_id=False) 2937 2937 >>> print p.as_ul() 2938 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2939 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2938 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2939 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2940 2940 >>> p = UserRegistration({'username': u''}, initial={'username': initial_django}, auto_id=False) 2941 2941 >>> print p.as_ul() 2942 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2943 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2942 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2943 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2944 2944 >>> p = UserRegistration({'username': u'foo'}, initial={'username': initial_django}, auto_id=False) 2945 2945 >>> print p.as_ul() 2946 <li >Username: <input type="text" name="username" value="foo" maxlength="10" /></li>2947 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2946 <li class="required">Username: <input type="text" name="username" value="foo" maxlength="10" /></li> 2947 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2948 2948 2949 2949 A callable 'initial' value is *not* used as a fallback if data is not provided. 2950 2950 In this example, we don't provide a value for 'username', and the form raises a … … 2962 2962 ... password = CharField(widget=PasswordInput) 2963 2963 >>> p = UserRegistration(auto_id=False) 2964 2964 >>> print p.as_ul() 2965 <li >Username: <input type="text" name="username" value="django" maxlength="10" /></li>2966 <li >Password: <input type="password" name="password" /></li>2965 <li class="required">Username: <input type="text" name="username" value="django" maxlength="10" /></li> 2966 <li class="required">Password: <input type="password" name="password" /></li> 2967 2967 >>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False) 2968 2968 >>> print p.as_ul() 2969 <li >Username: <input type="text" name="username" value="stephane" maxlength="10" /></li>2970 <li >Password: <input type="password" name="password" /></li>2969 <li class="required">Username: <input type="text" name="username" value="stephane" maxlength="10" /></li> 2970 <li class="required">Password: <input type="password" name="password" /></li> 2971 2971 2972 2972 # Help text ################################################################### 2973 2973 … … 2978 2978 ... password = CharField(widget=PasswordInput, help_text='Choose wisely.') 2979 2979 >>> p = UserRegistration(auto_id=False) 2980 2980 >>> print p.as_ul() 2981 <li >Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>2982 <li >Password: <input type="password" name="password" /> Choose wisely.</li>2981 <li class="required">Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li> 2982 <li class="required">Password: <input type="password" name="password" /> Choose wisely.</li> 2983 2983 >>> print p.as_p() 2984 <p >Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</p>2985 <p >Password: <input type="password" name="password" /> Choose wisely.</p>2984 <p class="required">Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</p> 2985 <p class="required">Password: <input type="password" name="password" /> Choose wisely.</p> 2986 2986 >>> print p.as_table() 2987 <tr ><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br />e.g., user@example.com</td></tr>2988 <tr ><th>Password:</th><td><input type="password" name="password" /><br />Choose wisely.</td></tr>2987 <tr class="required"><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br />e.g., user@example.com</td></tr> 2988 <tr class="required"><th>Password:</th><td><input type="password" name="password" /><br />Choose wisely.</td></tr> 2989 2989 2990 2990 The help text is displayed whether or not data is provided for the form. 2991 2991 >>> p = UserRegistration({'username': u'foo'}, auto_id=False) 2992 2992 >>> print p.as_ul() 2993 <li >Username: <input type="text" name="username" value="foo" maxlength="10" /> e.g., user@example.com</li>2994 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> Choose wisely.</li>2993 <li class="required">Username: <input type="text" name="username" value="foo" maxlength="10" /> e.g., user@example.com</li> 2994 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> Choose wisely.</li> 2995 2995 2996 2996 help_text is not displayed for hidden fields. It can be used for documentation 2997 2997 purposes, though. … … 3001 3001 ... next = CharField(widget=HiddenInput, initial='/', help_text='Redirect destination') 3002 3002 >>> p = UserRegistration(auto_id=False) 3003 3003 >>> print p.as_ul() 3004 <li >Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>3005 <li >Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li>3004 <li class="required">Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li> 3005 <li class="required">Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li> 3006 3006 3007 3007 Help text can include arbitrary Unicode characters. 3008 3008 >>> class UserRegistration(Form): 3009 3009 ... username = CharField(max_length=10, help_text='ŠĐĆŽćžšđ') 3010 3010 >>> p = UserRegistration(auto_id=False) 3011 3011 >>> p.as_ul() 3012 u'<li >Username: <input type="text" name="username" maxlength="10" /> \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</li>'3012 u'<li class="required">Username: <input type="text" name="username" maxlength="10" /> \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</li>' 3013 3013 3014 3014 # Subclassing forms ########################################################### 3015 3015 … … 3024 3024 ... instrument = CharField() 3025 3025 >>> p = Person(auto_id=False) 3026 3026 >>> print p.as_ul() 3027 <li >First name: <input type="text" name="first_name" /></li>3028 <li >Last name: <input type="text" name="last_name" /></li>3029 <li >Birthday: <input type="text" name="birthday" /></li>3027 <li class="required">First name: <input type="text" name="first_name" /></li> 3028 <li class="required">Last name: <input type="text" name="last_name" /></li> 3029 <li class="required">Birthday: <input type="text" name="birthday" /></li> 3030 3030 >>> m = Musician(auto_id=False) 3031 3031 >>> print m.as_ul() 3032 <li >First name: <input type="text" name="first_name" /></li>3033 <li >Last name: <input type="text" name="last_name" /></li>3034 <li >Birthday: <input type="text" name="birthday" /></li>3035 <li >Instrument: <input type="text" name="instrument" /></li>3032 <li class="required">First name: <input type="text" name="first_name" /></li> 3033 <li class="required">Last name: <input type="text" name="last_name" /></li> 3034 <li class="required">Birthday: <input type="text" name="birthday" /></li> 3035 <li class="required">Instrument: <input type="text" name="instrument" /></li> 3036 3036 3037 3037 Yes, you can subclass multiple forms. The fields are added in the order in 3038 3038 which the parent classes are listed. … … 3046 3046 ... haircut_type = CharField() 3047 3047 >>> b = Beatle(auto_id=False) 3048 3048 >>> print b.as_ul() 3049 <li >First name: <input type="text" name="first_name" /></li>3050 <li >Last name: <input type="text" name="last_name" /></li>3051 <li >Birthday: <input type="text" name="birthday" /></li>3052 <li >Instrument: <input type="text" name="instrument" /></li>3053 <li >Haircut type: <input type="text" name="haircut_type" /></li>3049 <li class="required">First name: <input type="text" name="first_name" /></li> 3050 <li class="required">Last name: <input type="text" name="last_name" /></li> 3051 <li class="required">Birthday: <input type="text" name="birthday" /></li> 3052 <li class="required">Instrument: <input type="text" name="instrument" /></li> 3053 <li class="required">Haircut type: <input type="text" name="haircut_type" /></li> 3054 3054 3055 3055 # Forms with prefixes ######################################################### 3056 3056 … … 3072 3072 ... } 3073 3073 >>> p = Person(data, prefix='person1') 3074 3074 >>> print p.as_ul() 3075 <li ><label for="id_person1-first_name">First name:</label> <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /></li>3076 <li ><label for="id_person1-last_name">Last name:</label> <input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" /></li>3077 <li ><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" /></li>3075 <li class="required"><label for="id_person1-first_name">First name:</label> <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /></li> 3076 <li class="required"><label for="id_person1-last_name">Last name:</label> <input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" /></li> 3077 <li class="required"><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" /></li> 3078 3078 >>> print p['first_name'] 3079 3079 <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /> 3080 3080 >>> print p['last_name'] … … 3149 3149 ... return self.prefix and '%s-prefix-%s' % (self.prefix, field_name) or field_name 3150 3150 >>> p = Person(prefix='foo') 3151 3151 >>> print p.as_ul() 3152 <li ><label for="id_foo-prefix-first_name">First name:</label> <input type="text" name="foo-prefix-first_name" id="id_foo-prefix-first_name" /></li>3153 <li ><label for="id_foo-prefix-last_name">Last name:</label> <input type="text" name="foo-prefix-last_name" id="id_foo-prefix-last_name" /></li>3154 <li ><label for="id_foo-prefix-birthday">Birthday:</label> <input type="text" name="foo-prefix-birthday" id="id_foo-prefix-birthday" /></li>3152 <li class="required"><label for="id_foo-prefix-first_name">First name:</label> <input type="text" name="foo-prefix-first_name" id="id_foo-prefix-first_name" /></li> 3153 <li class="required"><label for="id_foo-prefix-last_name">Last name:</label> <input type="text" name="foo-prefix-last_name" id="id_foo-prefix-last_name" /></li> 3154 <li class="required"><label for="id_foo-prefix-birthday">Birthday:</label> <input type="text" name="foo-prefix-birthday" id="id_foo-prefix-birthday" /></li> 3155 3155 >>> data = { 3156 3156 ... 'foo-prefix-first_name': u'John', 3157 3157 ... 'foo-prefix-last_name': u'Lennon', … … 3239 3239 >>> print my_function('GET', {}) 3240 3240 <form action="" method="post"> 3241 3241 <table> 3242 <tr ><th>Username:</th><td><input type="text" name="username" maxlength="10" /></td></tr>3243 <tr ><th>Password1:</th><td><input type="password" name="password1" /></td></tr>3244 <tr ><th>Password2:</th><td><input type="password" name="password2" /></td></tr>3242 <tr class="required"><th>Username:</th><td><input type="text" name="username" maxlength="10" /></td></tr> 3243 <tr class="required"><th>Password1:</th><td><input type="password" name="password1" /></td></tr> 3244 <tr class="required"><th>Password2:</th><td><input type="password" name="password2" /></td></tr> 3245 3245 </table> 3246 3246 <input type="submit" /> 3247 3247 </form> … … 3251 3251 <form action="" method="post"> 3252 3252 <table> 3253 3253 <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr> 3254 <tr ><th>Username:</th><td><ul class="errorlist"><li>Ensure this value has at most 10 characters.</li></ul><input type="text" name="username" value="this-is-a-long-username" maxlength="10" /></td></tr>3255 <tr ><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr>3256 <tr ><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr>3254 <tr class="error required"><th>Username:</th><td><ul class="errorlist"><li>Ensure this value has at most 10 characters.</li></ul><input type="text" name="username" value="this-is-a-long-username" maxlength="10" /></td></tr> 3255 <tr class="required"><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr> 3256 <tr class="required"><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr> 3257 3257 </table> 3258 3258 <input type="submit" /> 3259 3259 </form> … … 3601 3601 ... field1 = ComplexField(widget=w) 3602 3602 >>> f = ComplexFieldForm() 3603 3603 >>> print f 3604 <tr ><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" />3604 <tr class="required"><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" id="id_field1_0" /> 3605 3605 <select multiple="multiple" name="field1_1" id="id_field1_1"> 3606 3606 <option value="J">John</option> 3607 3607 <option value="P">Paul</option> … … 3612 3612 3613 3613 >>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'}) 3614 3614 >>> print f 3615 <tr ><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" />3615 <tr class="required"><th><label for="id_field1_0">Field1:</label></th><td><input type="text" name="field1_0" value="some text" id="id_field1_0" /> 3616 3616 <select multiple="multiple" name="field1_1" id="id_field1_1"> 3617 3617 <option value="J" selected="selected">John</option> 3618 3618 <option value="P" selected="selected">Paul</option>