Ticket #3512: html_class4.diff
File html_class4.diff, 93.9 KB (added by , 18 years ago) |
---|
-
django/newforms/forms.py
107 107 """ 108 108 return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name 109 109 110 def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row ):110 def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row, html_class_list=None): 111 111 "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." 112 112 top_errors = self.non_field_errors() # Errors that should be displayed above all fields. 113 113 output, hidden_fields = [], [] 114 html_class_list = html_class_list or [] 114 115 for name, field in self.fields.items(): 115 116 bf = BoundField(self, field, name) 116 117 bf_errors = ErrorList([escape(error) for error in bf.errors]) # Escape and cache in local variable. … … 119 120 top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors]) 120 121 hidden_fields.append(unicode(bf)) 121 122 else: 123 class_list = list(html_class_list) # A fresh list for each loop. 124 if bf_errors: 125 class_list.append('error') 126 if bf.field.required: 127 class_list.append('required') 128 if class_list: 129 html_class = ' class="%s"' % ' '.join(class_list) 130 else: 131 html_class = '' 122 132 if errors_on_separate_row and bf_errors: 123 133 output.append(error_row % bf_errors) 124 134 label = bf.label and bf.label_tag(escape(bf.label + ':')) or '' … … 126 136 help_text = help_text_html % field.help_text 127 137 else: 128 138 help_text = u'' 129 output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text })139 output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text, 'html_class': html_class}) 130 140 if top_errors: 131 141 output.insert(0, error_row % top_errors) 132 142 if hidden_fields: # Insert any hidden fields in the last row. … … 141 151 142 152 def as_table(self): 143 153 "Returns this form rendered as HTML <tr>s -- excluding the <table></table>." 144 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)154 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) 145 155 146 156 def as_ul(self): 147 157 "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>." 148 return self._html_output(u'<li >%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)158 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) 149 159 150 160 def as_p(self): 151 161 "Returns this form rendered as HTML <p>s." 152 return self._html_output(u'<p >%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True)162 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) 153 163 154 164 def non_field_errors(self): 155 165 """ -
docs/newforms.txt
293 293 294 294 >>> f = ContactForm() 295 295 >>> print f 296 <tr ><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>297 <tr ><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>298 <tr ><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>299 <tr ><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>296 <tr class="required"><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr> 297 <tr class="required"><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr> 298 <tr class="required"><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> 299 <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> 300 300 301 301 If the form is bound to data, the HTML output will include that data 302 302 appropriately. For example, if a field is represented by an … … 310 310 ... 'cc_myself': True} 311 311 >>> f = ContactForm(data) 312 312 >>> print f 313 <tr ><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" value="hello" /></td></tr>314 <tr ><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" value="Hi there" /></td></tr>315 <tr ><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" value="foo@example.com" /></td></tr>316 <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>313 <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> 314 <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> 315 <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> 316 <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> 317 317 318 318 This default output is a two-column HTML table, with a ``<tr>`` for each field. 319 319 Notice the following: … … 354 354 355 355 >>> f = ContactForm() 356 356 >>> f.as_p() 357 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>'357 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>' 358 358 >>> print f.as_p() 359 <p ><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p>360 <p ><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p>361 <p ><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>362 <p ><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>359 <p class="required"><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></p> 360 <p class="required"><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></p> 361 <p class="required"><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p> 362 <p class="required"><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p> 363 363 364 364 ``as_ul()`` 365 365 ~~~~~~~~~~~ … … 370 370 371 371 >>> f = ContactForm() 372 372 >>> f.as_ul() 373 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>'373 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>' 374 374 >>> print f.as_ul() 375 <li ><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li>376 <li ><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li>377 <li ><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>378 <li ><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>375 <li class="required"><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></li> 376 <li class="required"><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></li> 377 <li class="required"><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li> 378 <li class="required"><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li> 379 379 380 380 ``as_table()`` 381 381 ~~~~~~~~~~~~~~ … … 386 386 387 387 >>> f = ContactForm() 388 388 >>> f.as_table() 389 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>'389 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>' 390 390 >>> print f.as_table() 391 <tr ><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr>392 <tr ><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr>393 <tr ><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>394 <tr ><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>391 <tr class="required"><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" /></td></tr> 392 <tr class="required"><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" /></td></tr> 393 <tr class="required"><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> 394 <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> 395 395 396 396 Configuring HTML ``<label>`` tags 397 397 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 414 414 415 415 >>> f = ContactForm(auto_id=False) 416 416 >>> print f.as_table() 417 <tr ><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /></td></tr>418 <tr ><th>Message:</th><td><input type="text" name="message" /></td></tr>419 <tr ><th>Sender:</th><td><input type="text" name="sender" /></td></tr>420 <tr ><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr>417 <tr class="required"><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /></td></tr> 418 <tr class="required"><th>Message:</th><td><input type="text" name="message" /></td></tr> 419 <tr class="required"><th>Sender:</th><td><input type="text" name="sender" /></td></tr> 420 <tr class="required"><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr> 421 421 >>> print f.as_ul() 422 <li >Subject: <input type="text" name="subject" maxlength="100" /></li>423 <li >Message: <input type="text" name="message" /></li>424 <li >Sender: <input type="text" name="sender" /></li>425 <li >Cc myself: <input type="checkbox" name="cc_myself" /></li>422 <li class="required">Subject: <input type="text" name="subject" maxlength="100" /></li> 423 <li class="required">Message: <input type="text" name="message" /></li> 424 <li class="required">Sender: <input type="text" name="sender" /></li> 425 <li class="required">Cc myself: <input type="checkbox" name="cc_myself" /></li> 426 426 >>> print f.as_p() 427 <p >Subject: <input type="text" name="subject" maxlength="100" /></p>428 <p >Message: <input type="text" name="message" /></p>429 <p >Sender: <input type="text" name="sender" /></p>430 <p >Cc myself: <input type="checkbox" name="cc_myself" /></p>427 <p class="required">Subject: <input type="text" name="subject" maxlength="100" /></p> 428 <p class="required">Message: <input type="text" name="message" /></p> 429 <p class="required">Sender: <input type="text" name="sender" /></p> 430 <p class="required">Cc myself: <input type="checkbox" name="cc_myself" /></p> 431 431 432 432 If ``auto_id`` is set to ``True``, then the form output *will* include 433 433 ``<label>`` tags and will simply use the field name as its ``id`` for each form … … 435 435 436 436 >>> f = ContactForm(auto_id=True) 437 437 >>> print f.as_table() 438 <tr ><th><label for="subject">Subject:</label></th><td><input id="subject" type="text" name="subject" maxlength="100" /></td></tr>439 <tr ><th><label for="message">Message:</label></th><td><input type="text" name="message" id="message" /></td></tr>440 <tr ><th><label for="sender">Sender:</label></th><td><input type="text" name="sender" id="sender" /></td></tr>441 <tr ><th><label for="cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="cc_myself" /></td></tr>438 <tr class="required"><th><label for="subject">Subject:</label></th><td><input id="subject" type="text" name="subject" maxlength="100" /></td></tr> 439 <tr class="required"><th><label for="message">Message:</label></th><td><input type="text" name="message" id="message" /></td></tr> 440 <tr class="required"><th><label for="sender">Sender:</label></th><td><input type="text" name="sender" id="sender" /></td></tr> 441 <tr class="required"><th><label for="cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="cc_myself" /></td></tr> 442 442 >>> print f.as_ul() 443 <li ><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></li>444 <li ><label for="message">Message:</label> <input type="text" name="message" id="message" /></li>445 <li ><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></li>446 <li ><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></li>443 <li class="required"><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></li> 444 <li class="required"><label for="message">Message:</label> <input type="text" name="message" id="message" /></li> 445 <li class="required"><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></li> 446 <li class="required"><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></li> 447 447 >>> print f.as_p() 448 <p ><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></p>449 <p ><label for="message">Message:</label> <input type="text" name="message" id="message" /></p>450 <p ><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></p>451 <p ><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></p>448 <p class="required"><label for="subject">Subject:</label> <input id="subject" type="text" name="subject" maxlength="100" /></p> 449 <p class="required"><label for="message">Message:</label> <input type="text" name="message" id="message" /></p> 450 <p class="required"><label for="sender">Sender:</label> <input type="text" name="sender" id="sender" /></p> 451 <p class="required"><label for="cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="cc_myself" /></p> 452 452 453 453 If ``auto_id`` is set to a string containing the format character ``'%s'``, 454 454 then the form output will include ``<label>`` tags, and will generate ``id`` … … 458 458 459 459 >>> f = ContactForm(auto_id='id_for_%s') 460 460 >>> print f.as_table() 461 <tr ><th><label for="id_for_subject">Subject:</label></th><td><input id="id_for_subject" type="text" name="subject" maxlength="100" /></td></tr>462 <tr ><th><label for="id_for_message">Message:</label></th><td><input type="text" name="message" id="id_for_message" /></td></tr>463 <tr ><th><label for="id_for_sender">Sender:</label></th><td><input type="text" name="sender" id="id_for_sender" /></td></tr>464 <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>461 <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> 462 <tr class="required"><th><label for="id_for_message">Message:</label></th><td><input type="text" name="message" id="id_for_message" /></td></tr> 463 <tr class="required"><th><label for="id_for_sender">Sender:</label></th><td><input type="text" name="sender" id="id_for_sender" /></td></tr> 464 <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> 465 465 >>> print f.as_ul() 466 <li ><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li>467 <li ><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></li>468 <li ><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></li>469 <li ><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li>466 <li class="required"><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li> 467 <li class="required"><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></li> 468 <li class="required"><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></li> 469 <li class="required"><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li> 470 470 >>> print f.as_p() 471 <p ><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></p>472 <p ><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></p>473 <p ><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></p>474 <p ><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></p>471 <p class="required"><label for="id_for_subject">Subject:</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></p> 472 <p class="required"><label for="id_for_message">Message:</label> <input type="text" name="message" id="id_for_message" /></p> 473 <p class="required"><label for="id_for_sender">Sender:</label> <input type="text" name="sender" id="id_for_sender" /></p> 474 <p class="required"><label for="id_for_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></p> 475 475 476 476 If ``auto_id`` is set to any other true value -- such as a string that doesn't 477 477 include ``%s`` -- then the library will act as if ``auto_id`` is ``True``. … … 501 501 ... 'cc_myself': True} 502 502 >>> f = ContactForm(data, auto_id=False) 503 503 >>> print f.as_table() 504 <tr ><th>Subject:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="subject" maxlength="100" /></td></tr>505 <tr ><th>Message:</th><td><input type="text" name="message" value="Hi there" /></td></tr>506 <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>507 <tr ><th>Cc myself:</th><td><input checked="checked" type="checkbox" name="cc_myself" /></td></tr>504 <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> 505 <tr class="required"><th>Message:</th><td><input type="text" name="message" value="Hi there" /></td></tr> 506 <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> 507 <tr class="required"><th>Cc myself:</th><td><input checked="checked" type="checkbox" name="cc_myself" /></td></tr> 508 508 >>> print f.as_ul() 509 <li ><ul class="errorlist"><li>This field is required.</li></ul>Subject: <input type="text" name="subject" maxlength="100" /></li>510 <li >Message: <input type="text" name="message" value="Hi there" /></li>511 <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>512 <li >Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></li>509 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Subject: <input type="text" name="subject" maxlength="100" /></li> 510 <li class="required">Message: <input type="text" name="message" value="Hi there" /></li> 511 <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> 512 <li class="required">Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></li> 513 513 >>> print f.as_p() 514 514 <p><ul class="errorlist"><li>This field is required.</li></ul></p> 515 <p >Subject: <input type="text" name="subject" maxlength="100" /></p>516 <p >Message: <input type="text" name="message" value="Hi there" /></p>515 <p class="error required">Subject: <input type="text" name="subject" maxlength="100" /></p> 516 <p class="required">Message: <input type="text" name="message" value="Hi there" /></p> 517 517 <p><ul class="errorlist"><li>Enter a valid e-mail address.</li></ul></p> 518 <p >Sender: <input type="text" name="sender" value="invalid e-mail address" /></p>519 <p >Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p>518 <p class="error required">Sender: <input type="text" name="sender" value="invalid e-mail address" /></p> 519 <p class="required">Cc myself: <input checked="checked" type="checkbox" name="cc_myself" /></p> 520 520 521 You will notice that each row which is required is given the HTML class name 522 ``required`` and any row which the field did not pass validation receives the 523 ``error`` class name. 524 521 525 More granular output 522 526 ~~~~~~~~~~~~~~~~~~~~ 523 527 … … 587 591 ... priority = forms.CharField() 588 592 >>> f = ContactFormWithPriority(auto_id=False) 589 593 >>> print f.as_ul() 590 <li >Subject: <input type="text" name="subject" maxlength="100" /></li>591 <li >Message: <input type="text" name="message" /></li>592 <li >Sender: <input type="text" name="sender" /></li>593 <li >Cc myself: <input type="checkbox" name="cc_myself" /></li>594 <li >Priority: <input type="text" name="priority" /></li>594 <li class="required">Subject: <input type="text" name="subject" maxlength="100" /></li> 595 <li class="required">Message: <input type="text" name="message" /></li> 596 <li class="required">Sender: <input type="text" name="sender" /></li> 597 <li class="required">Cc myself: <input type="checkbox" name="cc_myself" /></li> 598 <li class="required">Priority: <input type="text" name="priority" /></li> 595 599 596 600 It's possible to subclass multiple forms, treating forms as "mix-ins." In this 597 601 example, ``BeatleForm`` subclasses both ``PersonForm`` and ``InstrumentForm`` … … 607 611 ... haircut_type = CharField() 608 612 >>> b = BeatleForm(auto_id=False) 609 613 >>> print b.as_ul() 610 <li >First name: <input type="text" name="first_name" /></li>611 <li >Last name: <input type="text" name="last_name" /></li>612 <li >Instrument: <input type="text" name="instrument" /></li>613 <li >Haircut type: <input type="text" name="haircut_type" /></li>614 <li class="required">First name: <input type="text" name="first_name" /></li> 615 <li class="required">Last name: <input type="text" name="last_name" /></li> 616 <li class="required">Instrument: <input type="text" name="instrument" /></li> 617 <li class="required">Haircut type: <input type="text" name="haircut_type" /></li> 614 618 615 619 Fields 616 620 ====== … … 717 721 ... comment = forms.CharField() 718 722 >>> f = CommentForm(auto_id=False) 719 723 >>> print f 720 <tr ><th>Your name:</th><td><input type="text" name="name" /></td></tr>724 <tr class="required"><th>Your name:</th><td><input type="text" name="name" /></td></tr> 721 725 <tr><th>Your Web site:</th><td><input type="text" name="url" /></td></tr> 722 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>726 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 723 727 724 728 ``initial`` 725 729 ~~~~~~~~~~~ … … 736 740 ... comment = forms.CharField() 737 741 >>> f = CommentForm(auto_id=False) 738 742 >>> print f 739 <tr ><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr>740 <tr ><th>Url:</th><td><input type="text" name="url" value="http://" /></td></tr>741 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>743 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr> 744 <tr class="required"><th>Url:</th><td><input type="text" name="url" value="http://" /></td></tr> 745 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 742 746 743 747 You may be thinking, why not just pass a dictionary of the initial values as 744 748 data when displaying the form? Well, if you do that, you'll trigger validation, … … 751 755 >>> default_data = {'name': 'Your name', 'url': 'http://'} 752 756 >>> f = CommentForm(default_data, auto_id=False) 753 757 >>> print f 754 <tr ><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr>755 <tr ><th>Url:</th><td><ul class="errorlist"><li>Enter a valid URL.</li></ul><input type="text" name="url" value="http://" /></td></tr>756 <tr ><th>Comment:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="comment" /></td></tr>758 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr> 759 <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> 760 <tr class="required"><th>Comment:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="comment" /></td></tr> 757 761 758 762 This is why ``initial`` values are only displayed for unbound forms. For bound 759 763 forms, the HTML output will use the bound data. … … 797 801 ... cc_myself = forms.BooleanField() 798 802 >>> f = HelpTextContactForm(auto_id=False) 799 803 >>> print f.as_table() 800 <tr ><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /><br />100 characters max.</td></tr>801 <tr ><th>Message:</th><td><input type="text" name="message" /></td></tr>802 <tr ><th>Sender:</th><td><input type="text" name="sender" /><br />A valid e-mail address, please.</td></tr>803 <tr ><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr>804 <tr class="required"><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /><br />100 characters max.</td></tr> 805 <tr class="required"><th>Message:</th><td><input type="text" name="message" /></td></tr> 806 <tr class="required"><th>Sender:</th><td><input type="text" name="sender" /><br />A valid e-mail address, please.</td></tr> 807 <tr class="required"><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr> 804 808 >>> print f.as_ul() 805 <li >Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</li>806 <li >Message: <input type="text" name="message" /></li>807 <li >Sender: <input type="text" name="sender" /> A valid e-mail address, please.</li>808 <li >Cc myself: <input type="checkbox" name="cc_myself" /></li>809 <li class="required">Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</li> 810 <li class="required">Message: <input type="text" name="message" /></li> 811 <li class="required">Sender: <input type="text" name="sender" /> A valid e-mail address, please.</li> 812 <li class="required">Cc myself: <input type="checkbox" name="cc_myself" /></li> 809 813 >>> print f.as_p() 810 <p >Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</p>811 <p >Message: <input type="text" name="message" /></p>812 <p >Sender: <input type="text" name="sender" /> A valid e-mail address, please.</p>813 <p >Cc myself: <input type="checkbox" name="cc_myself" /></p>814 <p class="required">Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</p> 815 <p class="required">Message: <input type="text" name="message" /></p> 816 <p class="required">Sender: <input type="text" name="sender" /> A valid e-mail address, please.</p> 817 <p class="required">Cc myself: <input type="checkbox" name="cc_myself" /></p> 814 818 815 819 Dynamic initial values 816 820 ---------------------- … … 831 835 ... comment = forms.CharField() 832 836 >>> f = CommentForm(initial={'name': 'your username'}, auto_id=False) 833 837 >>> print f 834 <tr ><th>Name:</th><td><input type="text" name="name" value="your username" /></td></tr>835 <tr ><th>Url:</th><td><input type="text" name="url" /></td></tr>836 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>838 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="your username" /></td></tr> 839 <tr class="required"><th>Url:</th><td><input type="text" name="url" /></td></tr> 840 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 837 841 >>> f = CommentForm(initial={'name': 'another username'}, auto_id=False) 838 842 >>> print f 839 <tr ><th>Name:</th><td><input type="text" name="name" value="another username" /></td></tr>840 <tr ><th>Url:</th><td><input type="text" name="url" /></td></tr>841 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>843 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="another username" /></td></tr> 844 <tr class="required"><th>Url:</th><td><input type="text" name="url" /></td></tr> 845 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 842 846 843 847 Just like the ``initial`` parameter to ``Field``, these values are only 844 848 displayed for unbound forms, and they're not used as fallback values if a … … 855 859 ... comment = forms.CharField() 856 860 >>> f = CommentForm(initial={'name': 'instance'}, auto_id=False) 857 861 >>> print f 858 <tr ><th>Name:</th><td><input type="text" name="name" value="instance" /></td></tr>859 <tr ><th>Url:</th><td><input type="text" name="url" /></td></tr>860 <tr ><th>Comment:</th><td><input type="text" name="comment" /></td></tr>862 <tr class="required"><th>Name:</th><td><input type="text" name="name" value="instance" /></td></tr> 863 <tr class="required"><th>Url:</th><td><input type="text" name="url" /></td></tr> 864 <tr class="required"><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 861 865 862 866 More coming soon 863 867 ================ -
tests/regressiontests/forms/tests.py
335 335 ... somechoice = ChoiceField(choices=chain((('', '-'*9),), [(thing['id'], thing['name']) for thing in things])) 336 336 >>> f = SomeForm() 337 337 >>> f.as_table() 338 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>'338 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>' 339 339 >>> f.as_table() 340 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>'340 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>' 341 341 >>> f = SomeForm({'somechoice': 2}) 342 342 >>> f.as_table() 343 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>'343 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>' 344 344 345 345 You can also pass 'choices' to the constructor: 346 346 >>> w = Select(choices=[(1, 1), (2, 2), (3, 3)]) … … 1768 1768 Last name Lennon 1769 1769 Birthday 1940-10-9 1770 1770 >>> print p 1771 <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>1772 <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>1773 <tr ><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>1771 <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> 1772 <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> 1773 <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> 1774 1774 1775 1775 Empty dictionaries are valid, too. 1776 1776 >>> p = Person({}) … … 1785 1785 ... 1786 1786 AttributeError: 'Person' object has no attribute 'clean_data' 1787 1787 >>> print p 1788 <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>1789 <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>1790 <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>1788 <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> 1789 <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> 1790 <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> 1791 1791 >>> print p.as_table() 1792 <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>1793 <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>1794 <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>1792 <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> 1793 <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> 1794 <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> 1795 1795 >>> print p.as_ul() 1796 <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>1797 <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>1798 <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>1796 <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> 1797 <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> 1798 <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> 1799 1799 >>> print p.as_p() 1800 1800 <p><ul class="errorlist"><li>This field is required.</li></ul></p> 1801 <p ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>1801 <p class="error required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p> 1802 1802 <p><ul class="errorlist"><li>This field is required.</li></ul></p> 1803 <p ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>1803 <p class="error required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p> 1804 1804 <p><ul class="errorlist"><li>This field is required.</li></ul></p> 1805 <p ><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>1805 <p class="error required"><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p> 1806 1806 1807 1807 If you don't pass any values to the Form's __init__(), or if you pass None, 1808 1808 the Form will be considered unbound and won't do any validation. Form.errors … … 1819 1819 ... 1820 1820 AttributeError: 'Person' object has no attribute 'clean_data' 1821 1821 >>> print p 1822 <tr ><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>1823 <tr ><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>1824 <tr ><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr>1822 <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> 1823 <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> 1824 <tr class="required"><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr> 1825 1825 >>> print p.as_table() 1826 <tr ><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>1827 <tr ><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>1828 <tr ><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr>1826 <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> 1827 <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> 1828 <tr class="required"><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr> 1829 1829 >>> print p.as_ul() 1830 <li ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>1831 <li ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>1832 <li ><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>1830 <li class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li> 1831 <li class="required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li> 1832 <li class="required"><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li> 1833 1833 >>> print p.as_p() 1834 <p ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>1835 <p ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>1836 <p ><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>1834 <p class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p> 1835 <p class="required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p> 1836 <p class="required"><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p> 1837 1837 1838 1838 Unicode values are handled properly. 1839 1839 >>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'}) 1840 1840 >>> p.as_table() 1841 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>'1841 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>' 1842 1842 >>> p.as_ul() 1843 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>'1843 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>' 1844 1844 >>> p.as_p() 1845 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>'1845 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>' 1846 1846 1847 1847 >>> p = Person({'last_name': u'Lennon'}) 1848 1848 >>> p.errors … … 1892 1892 the human-readable labels for a field. 1893 1893 >>> p = Person(auto_id='%s_id') 1894 1894 >>> print p.as_table() 1895 <tr ><th><label for="first_name_id">First name:</label></th><td><input type="text" name="first_name" id="first_name_id" /></td></tr>1896 <tr ><th><label for="last_name_id">Last name:</label></th><td><input type="text" name="last_name" id="last_name_id" /></td></tr>1897 <tr ><th><label for="birthday_id">Birthday:</label></th><td><input type="text" name="birthday" id="birthday_id" /></td></tr>1895 <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> 1896 <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> 1897 <tr class="required"><th><label for="birthday_id">Birthday:</label></th><td><input type="text" name="birthday" id="birthday_id" /></td></tr> 1898 1898 >>> print p.as_ul() 1899 <li ><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></li>1900 <li ><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></li>1901 <li ><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></li>1899 <li class="required"><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></li> 1900 <li class="required"><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></li> 1901 <li class="required"><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></li> 1902 1902 >>> print p.as_p() 1903 <p ><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></p>1904 <p ><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></p>1905 <p ><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></p>1903 <p class="required"><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></p> 1904 <p class="required"><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></p> 1905 <p class="required"><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></p> 1906 1906 1907 1907 If auto_id is any True value whose str() does not contain '%s', the "id" 1908 1908 attribute will be the name of the field. 1909 1909 >>> p = Person(auto_id=True) 1910 1910 >>> print p.as_ul() 1911 <li ><label for="first_name">First name:</label> <input type="text" name="first_name" id="first_name" /></li>1912 <li ><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li>1913 <li ><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li>1911 <li class="required"><label for="first_name">First name:</label> <input type="text" name="first_name" id="first_name" /></li> 1912 <li class="required"><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li> 1913 <li class="required"><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li> 1914 1914 1915 1915 If auto_id is any False value, an "id" attribute won't be output unless it 1916 1916 was manually entered. 1917 1917 >>> p = Person(auto_id=False) 1918 1918 >>> print p.as_ul() 1919 <li >First name: <input type="text" name="first_name" /></li>1920 <li >Last name: <input type="text" name="last_name" /></li>1921 <li >Birthday: <input type="text" name="birthday" /></li>1919 <li class="required">First name: <input type="text" name="first_name" /></li> 1920 <li class="required">Last name: <input type="text" name="last_name" /></li> 1921 <li class="required">Birthday: <input type="text" name="birthday" /></li> 1922 1922 1923 1923 In this example, auto_id is False, but the "id" attribute for the "first_name" 1924 1924 field is given. Also note that field gets a <label>, while the others don't. … … 1928 1928 ... birthday = DateField() 1929 1929 >>> p = PersonNew(auto_id=False) 1930 1930 >>> print p.as_ul() 1931 <li ><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li>1932 <li >Last name: <input type="text" name="last_name" /></li>1933 <li >Birthday: <input type="text" name="birthday" /></li>1931 <li class="required"><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li> 1932 <li class="required">Last name: <input type="text" name="last_name" /></li> 1933 <li class="required">Birthday: <input type="text" name="birthday" /></li> 1934 1934 1935 1935 If the "id" attribute is specified in the Form and auto_id is True, the "id" 1936 1936 attribute in the Form gets precedence. 1937 1937 >>> p = PersonNew(auto_id=True) 1938 1938 >>> print p.as_ul() 1939 <li ><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li>1940 <li ><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li>1941 <li ><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li>1939 <li class="required"><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li> 1940 <li class="required"><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li> 1941 <li class="required"><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li> 1942 1942 1943 1943 >>> class SignupForm(Form): 1944 1944 ... email = EmailField() … … 2086 2086 <li><label><input type="radio" name="language" value="J" /> Java</label></li> 2087 2087 </ul> 2088 2088 >>> print f 2089 <tr ><th>Name:</th><td><input type="text" name="name" /></td></tr>2090 <tr ><th>Language:</th><td><ul>2089 <tr class="required"><th>Name:</th><td><input type="text" name="name" /></td></tr> 2090 <tr class="required"><th>Language:</th><td><ul> 2091 2091 <li><label><input type="radio" name="language" value="P" /> Python</label></li> 2092 2092 <li><label><input type="radio" name="language" value="J" /> Java</label></li> 2093 2093 </ul></td></tr> 2094 2094 >>> print f.as_ul() 2095 <li >Name: <input type="text" name="name" /></li>2096 <li >Language: <ul>2095 <li class="required">Name: <input type="text" name="name" /></li> 2096 <li class="required">Language: <ul> 2097 2097 <li><label><input type="radio" name="language" value="P" /> Python</label></li> 2098 2098 <li><label><input type="radio" name="language" value="J" /> Java</label></li> 2099 2099 </ul></li> … … 2112 2112 either as_table() or as_ul(), the label for the RadioSelect will point to the 2113 2113 ID of the *first* radio button. 2114 2114 >>> print f 2115 <tr ><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>2116 <tr ><th><label for="id_language_0">Language:</label></th><td><ul>2115 <tr class="required"><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr> 2116 <tr class="required"><th><label for="id_language_0">Language:</label></th><td><ul> 2117 2117 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li> 2118 2118 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li> 2119 2119 </ul></td></tr> 2120 2120 >>> print f.as_ul() 2121 <li ><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>2122 <li ><label for="id_language_0">Language:</label> <ul>2121 <li class="required"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li> 2122 <li class="required"><label for="id_language_0">Language:</label> <ul> 2123 2123 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li> 2124 2124 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li> 2125 2125 </ul></li> 2126 2126 >>> print f.as_p() 2127 <p ><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>2128 <p ><label for="id_language_0">Language:</label> <ul>2127 <p class="required"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p> 2128 <p class="required"><label for="id_language_0">Language:</label> <ul> 2129 2129 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li> 2130 2130 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li> 2131 2131 </ul></p> … … 2223 2223 ... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=MultipleHiddenInput) 2224 2224 >>> f = SongFormHidden(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])), auto_id=False) 2225 2225 >>> print f.as_ul() 2226 <li >Name: <input type="text" name="name" value="Yesterday" /><input type="hidden" name="composers" value="J" />2226 <li class="required">Name: <input type="text" name="name" value="Yesterday" /><input type="hidden" name="composers" value="J" /> 2227 2227 <input type="hidden" name="composers" value="P" /></li> 2228 2228 2229 2229 When using CheckboxSelectMultiple, the framework expects a list of input and … … 2250 2250 2251 2251 >>> f = EscapingForm({'special_name': "Nothing to escape"}, auto_id=False) 2252 2252 >>> print f 2253 <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>2253 <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> 2254 2254 >>> f = EscapingForm({'special_name': "Should escape < & > and <script>alert('xss')</script>"}, auto_id=False) 2255 2255 >>> print f 2256 <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>2256 <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> 2257 2257 2258 2258 # Validating multiple fields in relation to another ########################### 2259 2259 … … 2307 2307 {} 2308 2308 >>> f = UserRegistration({}, auto_id=False) 2309 2309 >>> print f.as_table() 2310 <tr ><th>Username:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="username" maxlength="10" /></td></tr>2311 <tr ><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr>2312 <tr ><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr>2310 <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> 2311 <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> 2312 <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> 2313 2313 >>> f.errors 2314 2314 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} 2315 2315 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) … … 2317 2317 {'__all__': [u'Please make sure your passwords match.']} 2318 2318 >>> print f.as_table() 2319 2319 <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr> 2320 <tr ><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr>2321 <tr ><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr>2322 <tr ><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr>2320 <tr class="required"><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr> 2321 <tr class="required"><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr> 2322 <tr class="required"><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr> 2323 2323 >>> print f.as_ul() 2324 2324 <li><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></li> 2325 <li >Username: <input type="text" name="username" value="adrian" maxlength="10" /></li>2326 <li >Password1: <input type="password" name="password1" value="foo" /></li>2327 <li >Password2: <input type="password" name="password2" value="bar" /></li>2325 <li class="required">Username: <input type="text" name="username" value="adrian" maxlength="10" /></li> 2326 <li class="required">Password1: <input type="password" name="password1" value="foo" /></li> 2327 <li class="required">Password2: <input type="password" name="password2" value="bar" /></li> 2328 2328 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False) 2329 2329 >>> f.errors 2330 2330 {} … … 2344 2344 ... self.fields['birthday'] = DateField() 2345 2345 >>> p = Person(auto_id=False) 2346 2346 >>> print p 2347 <tr ><th>First name:</th><td><input type="text" name="first_name" /></td></tr>2348 <tr ><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>2349 <tr ><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>2347 <tr class="required"><th>First name:</th><td><input type="text" name="first_name" /></td></tr> 2348 <tr class="required"><th>Last name:</th><td><input type="text" name="last_name" /></td></tr> 2349 <tr class="required"><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr> 2350 2350 2351 2351 Instances of a dynamic Form do not persist fields from one Form instance to 2352 2352 the next. … … 2358 2358 >>> field_list = [('field1', CharField()), ('field2', CharField())] 2359 2359 >>> my_form = MyForm(field_list=field_list) 2360 2360 >>> print my_form 2361 <tr ><th>Field1:</th><td><input type="text" name="field1" /></td></tr>2362 <tr ><th>Field2:</th><td><input type="text" name="field2" /></td></tr>2361 <tr class="required"><th>Field1:</th><td><input type="text" name="field1" /></td></tr> 2362 <tr class="required"><th>Field2:</th><td><input type="text" name="field2" /></td></tr> 2363 2363 >>> field_list = [('field3', CharField()), ('field4', CharField())] 2364 2364 >>> my_form = MyForm(field_list=field_list) 2365 2365 >>> print my_form 2366 <tr ><th>Field3:</th><td><input type="text" name="field3" /></td></tr>2367 <tr ><th>Field4:</th><td><input type="text" name="field4" /></td></tr>2366 <tr class="required"><th>Field3:</th><td><input type="text" name="field3" /></td></tr> 2367 <tr class="required"><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 2368 2368 2369 2369 >>> class MyForm(Form): 2370 2370 ... default_field_1 = CharField() … … 2376 2376 >>> field_list = [('field1', CharField()), ('field2', CharField())] 2377 2377 >>> my_form = MyForm(field_list=field_list) 2378 2378 >>> print my_form 2379 <tr ><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr>2380 <tr ><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr>2381 <tr ><th>Field1:</th><td><input type="text" name="field1" /></td></tr>2382 <tr ><th>Field2:</th><td><input type="text" name="field2" /></td></tr>2379 <tr class="required"><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr> 2380 <tr class="required"><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr> 2381 <tr class="required"><th>Field1:</th><td><input type="text" name="field1" /></td></tr> 2382 <tr class="required"><th>Field2:</th><td><input type="text" name="field2" /></td></tr> 2383 2383 >>> field_list = [('field3', CharField()), ('field4', CharField())] 2384 2384 >>> my_form = MyForm(field_list=field_list) 2385 2385 >>> print my_form 2386 <tr ><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr>2387 <tr ><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr>2388 <tr ><th>Field3:</th><td><input type="text" name="field3" /></td></tr>2389 <tr ><th>Field4:</th><td><input type="text" name="field4" /></td></tr>2386 <tr class="required"><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr> 2387 <tr class="required"><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr> 2388 <tr class="required"><th>Field3:</th><td><input type="text" name="field3" /></td></tr> 2389 <tr class="required"><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 2390 2390 2391 2391 Similarly, changes to field attributes do not persist from one Form instance 2392 2392 to the next. … … 2436 2436 ... birthday = DateField() 2437 2437 >>> p = Person(auto_id=False) 2438 2438 >>> print p 2439 <tr ><th>First name:</th><td><input type="text" name="first_name" /></td></tr>2440 <tr ><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>2441 <tr ><th>Birthday:</th><td><input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></td></tr>2439 <tr class="required"><th>First name:</th><td><input type="text" name="first_name" /></td></tr> 2440 <tr class="required"><th>Last name:</th><td><input type="text" name="last_name" /></td></tr> 2441 <tr class="required"><th>Birthday:</th><td><input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></td></tr> 2442 2442 >>> print p.as_ul() 2443 <li >First name: <input type="text" name="first_name" /></li>2444 <li >Last name: <input type="text" name="last_name" /></li>2445 <li >Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></li>2443 <li class="required">First name: <input type="text" name="first_name" /></li> 2444 <li class="required">Last name: <input type="text" name="last_name" /></li> 2445 <li class="required">Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></li> 2446 2446 >>> print p.as_p() 2447 <p >First name: <input type="text" name="first_name" /></p>2448 <p >Last name: <input type="text" name="last_name" /></p>2449 <p >Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p>2447 <p class="required">First name: <input type="text" name="first_name" /></p> 2448 <p class="required">Last name: <input type="text" name="last_name" /></p> 2449 <p class="required">Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p> 2450 2450 2451 2451 With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label. 2452 2452 >>> p = Person(auto_id='id_%s') 2453 2453 >>> print p 2454 <tr ><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>2455 <tr ><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>2456 <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>2454 <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> 2455 <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> 2456 <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> 2457 2457 >>> print p.as_ul() 2458 <li ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>2459 <li ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>2460 <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>2458 <li class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li> 2459 <li class="required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li> 2460 <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> 2461 2461 >>> print p.as_p() 2462 <p ><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>2463 <p ><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>2464 <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>2462 <p class="required"><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p> 2463 <p class="required"><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p> 2464 <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> 2465 2465 2466 2466 If a field with a HiddenInput has errors, the as_table() and as_ul() output 2467 2467 will include the error message(s) with the text "(Hidden field [fieldname]) " … … 2470 2470 >>> p = Person({'first_name': 'John', 'last_name': 'Lennon', 'birthday': '1940-10-9'}, auto_id=False) 2471 2471 >>> print p 2472 2472 <tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr> 2473 <tr ><th>First name:</th><td><input type="text" name="first_name" value="John" /></td></tr>2474 <tr ><th>Last name:</th><td><input type="text" name="last_name" value="Lennon" /></td></tr>2475 <tr ><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></td></tr>2473 <tr class="required"><th>First name:</th><td><input type="text" name="first_name" value="John" /></td></tr> 2474 <tr class="required"><th>Last name:</th><td><input type="text" name="last_name" value="Lennon" /></td></tr> 2475 <tr class="required"><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></td></tr> 2476 2476 >>> print p.as_ul() 2477 2477 <li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li> 2478 <li >First name: <input type="text" name="first_name" value="John" /></li>2479 <li >Last name: <input type="text" name="last_name" value="Lennon" /></li>2480 <li >Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li>2478 <li class="required">First name: <input type="text" name="first_name" value="John" /></li> 2479 <li class="required">Last name: <input type="text" name="last_name" value="Lennon" /></li> 2480 <li class="required">Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li> 2481 2481 >>> print p.as_p() 2482 2482 <p><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></p> 2483 <p >First name: <input type="text" name="first_name" value="John" /></p>2484 <p >Last name: <input type="text" name="last_name" value="Lennon" /></p>2485 <p >Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>2483 <p class="required">First name: <input type="text" name="first_name" value="John" /></p> 2484 <p class="required">Last name: <input type="text" name="last_name" value="Lennon" /></p> 2485 <p class="required">Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p> 2486 2486 2487 2487 A corner case: It's possible for a form to have only HiddenInputs. 2488 2488 >>> class TestForm(Form): … … 2514 2514 ... field14 = CharField() 2515 2515 >>> p = TestForm(auto_id=False) 2516 2516 >>> print p 2517 <tr ><th>Field1:</th><td><input type="text" name="field1" /></td></tr>2518 <tr ><th>Field2:</th><td><input type="text" name="field2" /></td></tr>2519 <tr ><th>Field3:</th><td><input type="text" name="field3" /></td></tr>2520 <tr ><th>Field4:</th><td><input type="text" name="field4" /></td></tr>2521 <tr ><th>Field5:</th><td><input type="text" name="field5" /></td></tr>2522 <tr ><th>Field6:</th><td><input type="text" name="field6" /></td></tr>2523 <tr ><th>Field7:</th><td><input type="text" name="field7" /></td></tr>2524 <tr ><th>Field8:</th><td><input type="text" name="field8" /></td></tr>2525 <tr ><th>Field9:</th><td><input type="text" name="field9" /></td></tr>2526 <tr ><th>Field10:</th><td><input type="text" name="field10" /></td></tr>2527 <tr ><th>Field11:</th><td><input type="text" name="field11" /></td></tr>2528 <tr ><th>Field12:</th><td><input type="text" name="field12" /></td></tr>2529 <tr ><th>Field13:</th><td><input type="text" name="field13" /></td></tr>2530 <tr ><th>Field14:</th><td><input type="text" name="field14" /></td></tr>2517 <tr class="required"><th>Field1:</th><td><input type="text" name="field1" /></td></tr> 2518 <tr class="required"><th>Field2:</th><td><input type="text" name="field2" /></td></tr> 2519 <tr class="required"><th>Field3:</th><td><input type="text" name="field3" /></td></tr> 2520 <tr class="required"><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 2521 <tr class="required"><th>Field5:</th><td><input type="text" name="field5" /></td></tr> 2522 <tr class="required"><th>Field6:</th><td><input type="text" name="field6" /></td></tr> 2523 <tr class="required"><th>Field7:</th><td><input type="text" name="field7" /></td></tr> 2524 <tr class="required"><th>Field8:</th><td><input type="text" name="field8" /></td></tr> 2525 <tr class="required"><th>Field9:</th><td><input type="text" name="field9" /></td></tr> 2526 <tr class="required"><th>Field10:</th><td><input type="text" name="field10" /></td></tr> 2527 <tr class="required"><th>Field11:</th><td><input type="text" name="field11" /></td></tr> 2528 <tr class="required"><th>Field12:</th><td><input type="text" name="field12" /></td></tr> 2529 <tr class="required"><th>Field13:</th><td><input type="text" name="field13" /></td></tr> 2530 <tr class="required"><th>Field14:</th><td><input type="text" name="field14" /></td></tr> 2531 2531 2532 2532 Some Field classes have an effect on the HTML attributes of their associated 2533 2533 Widget. If you set max_length in a CharField and its associated widget is … … 2540 2540 ... address = CharField() # no max_length defined here 2541 2541 >>> p = UserRegistration(auto_id=False) 2542 2542 >>> print p.as_ul() 2543 <li >Username: <input type="text" name="username" maxlength="10" /></li>2544 <li >Password: <input type="password" name="password" maxlength="10" /></li>2545 <li >Realname: <input type="text" name="realname" maxlength="10" /></li>2546 <li >Address: <input type="text" name="address" /></li>2543 <li class="required">Username: <input type="text" name="username" maxlength="10" /></li> 2544 <li class="required">Password: <input type="password" name="password" maxlength="10" /></li> 2545 <li class="required">Realname: <input type="text" name="realname" maxlength="10" /></li> 2546 <li class="required">Address: <input type="text" name="address" /></li> 2547 2547 2548 2548 If you specify a custom "attrs" that includes the "maxlength" attribute, 2549 2549 the Field's max_length attribute will override whatever "maxlength" you specify … … 2553 2553 ... password = CharField(max_length=10, widget=PasswordInput) 2554 2554 >>> p = UserRegistration(auto_id=False) 2555 2555 >>> print p.as_ul() 2556 <li >Username: <input type="text" name="username" maxlength="10" /></li>2557 <li >Password: <input type="password" name="password" maxlength="10" /></li>2556 <li class="required">Username: <input type="text" name="username" maxlength="10" /></li> 2557 <li class="required">Password: <input type="password" name="password" maxlength="10" /></li> 2558 2558 2559 2559 # Specifying labels ########################################################### 2560 2560 … … 2567 2567 ... password2 = CharField(widget=PasswordInput, label='Password (again)') 2568 2568 >>> p = UserRegistration(auto_id=False) 2569 2569 >>> print p.as_ul() 2570 <li >Your username: <input type="text" name="username" maxlength="10" /></li>2571 <li >Password1: <input type="password" name="password1" /></li>2572 <li >Password (again): <input type="password" name="password2" /></li>2570 <li class="required">Your username: <input type="text" name="username" maxlength="10" /></li> 2571 <li class="required">Password1: <input type="password" name="password1" /></li> 2572 <li class="required">Password (again): <input type="password" name="password2" /></li> 2573 2573 2574 2574 A label can be a Unicode object or a bytestring with special characters. 2575 2575 >>> class UserRegistration(Form): … … 2577 2577 ... password = CharField(widget=PasswordInput, label=u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') 2578 2578 >>> p = UserRegistration(auto_id=False) 2579 2579 >>> p.as_ul() 2580 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>'2580 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>' 2581 2581 2582 2582 If a label is set to the empty string for a field, that field won't get a label. 2583 2583 >>> class UserRegistration(Form): … … 2585 2585 ... password = CharField(widget=PasswordInput) 2586 2586 >>> p = UserRegistration(auto_id=False) 2587 2587 >>> print p.as_ul() 2588 <li > <input type="text" name="username" maxlength="10" /></li>2589 <li >Password: <input type="password" name="password" /></li>2588 <li class="required"> <input type="text" name="username" maxlength="10" /></li> 2589 <li class="required">Password: <input type="password" name="password" /></li> 2590 2590 >>> p = UserRegistration(auto_id='id_%s') 2591 2591 >>> print p.as_ul() 2592 <li > <input id="id_username" type="text" name="username" maxlength="10" /></li>2593 <li ><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li>2592 <li class="required"> <input id="id_username" type="text" name="username" maxlength="10" /></li> 2593 <li class="required"><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> 2594 2594 2595 2595 If label is None, Django will auto-create the label from the field name. This 2596 2596 is default behavior. … … 2599 2599 ... password = CharField(widget=PasswordInput) 2600 2600 >>> p = UserRegistration(auto_id=False) 2601 2601 >>> print p.as_ul() 2602 <li >Username: <input type="text" name="username" maxlength="10" /></li>2603 <li >Password: <input type="password" name="password" /></li>2602 <li class="required">Username: <input type="text" name="username" maxlength="10" /></li> 2603 <li class="required">Password: <input type="password" name="password" /></li> 2604 2604 >>> p = UserRegistration(auto_id='id_%s') 2605 2605 >>> print p.as_ul() 2606 <li ><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li>2607 <li ><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li>2606 <li class="required"><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li> 2607 <li class="required"><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> 2608 2608 2609 2609 # Initial data ################################################################ 2610 2610 … … 2620 2620 Here, we're not submitting any data, so the initial value will be displayed. 2621 2621 >>> p = UserRegistration(auto_id=False) 2622 2622 >>> print p.as_ul() 2623 <li >Username: <input type="text" name="username" value="django" maxlength="10" /></li>2624 <li >Password: <input type="password" name="password" /></li>2623 <li class="required">Username: <input type="text" name="username" value="django" maxlength="10" /></li> 2624 <li class="required">Password: <input type="password" name="password" /></li> 2625 2625 2626 2626 Here, we're submitting data, so the initial value will *not* be displayed. 2627 2627 >>> p = UserRegistration({}, auto_id=False) 2628 2628 >>> print p.as_ul() 2629 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2630 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2629 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2630 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2631 2631 >>> p = UserRegistration({'username': u''}, auto_id=False) 2632 2632 >>> print p.as_ul() 2633 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2634 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2633 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2634 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2635 2635 >>> p = UserRegistration({'username': u'foo'}, auto_id=False) 2636 2636 >>> print p.as_ul() 2637 <li >Username: <input type="text" name="username" value="foo" maxlength="10" /></li>2638 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2637 <li class="required">Username: <input type="text" name="username" value="foo" maxlength="10" /></li> 2638 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2639 2639 2640 2640 An 'initial' value is *not* used as a fallback if data is not provided. In this 2641 2641 example, we don't provide a value for 'username', and the form raises a … … 2661 2661 Here, we're not submitting any data, so the initial value will be displayed. 2662 2662 >>> p = UserRegistration(initial={'username': 'django'}, auto_id=False) 2663 2663 >>> print p.as_ul() 2664 <li >Username: <input type="text" name="username" value="django" maxlength="10" /></li>2665 <li >Password: <input type="password" name="password" /></li>2664 <li class="required">Username: <input type="text" name="username" value="django" maxlength="10" /></li> 2665 <li class="required">Password: <input type="password" name="password" /></li> 2666 2666 >>> p = UserRegistration(initial={'username': 'stephane'}, auto_id=False) 2667 2667 >>> print p.as_ul() 2668 <li >Username: <input type="text" name="username" value="stephane" maxlength="10" /></li>2669 <li >Password: <input type="password" name="password" /></li>2668 <li class="required">Username: <input type="text" name="username" value="stephane" maxlength="10" /></li> 2669 <li class="required">Password: <input type="password" name="password" /></li> 2670 2670 2671 2671 The 'initial' parameter is meaningless if you pass data. 2672 2672 >>> p = UserRegistration({}, initial={'username': 'django'}, auto_id=False) 2673 2673 >>> print p.as_ul() 2674 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2675 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2674 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2675 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2676 2676 >>> p = UserRegistration({'username': u''}, initial={'username': 'django'}, auto_id=False) 2677 2677 >>> print p.as_ul() 2678 <li ><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li>2679 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2678 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> 2679 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2680 2680 >>> p = UserRegistration({'username': u'foo'}, initial={'username': 'django'}, auto_id=False) 2681 2681 >>> print p.as_ul() 2682 <li >Username: <input type="text" name="username" value="foo" maxlength="10" /></li>2683 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li>2682 <li class="required">Username: <input type="text" name="username" value="foo" maxlength="10" /></li> 2683 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> 2684 2684 2685 2685 A dynamic 'initial' value is *not* used as a fallback if data is not provided. 2686 2686 In this example, we don't provide a value for 'username', and the form raises a … … 2698 2698 ... password = CharField(widget=PasswordInput) 2699 2699 >>> p = UserRegistration(initial={'username': 'babik'}, auto_id=False) 2700 2700 >>> print p.as_ul() 2701 <li >Username: <input type="text" name="username" value="babik" maxlength="10" /></li>2702 <li >Password: <input type="password" name="password" /></li>2701 <li class="required">Username: <input type="text" name="username" value="babik" maxlength="10" /></li> 2702 <li class="required">Password: <input type="password" name="password" /></li> 2703 2703 2704 2704 # Help text ################################################################### 2705 2705 … … 2710 2710 ... password = CharField(widget=PasswordInput, help_text='Choose wisely.') 2711 2711 >>> p = UserRegistration(auto_id=False) 2712 2712 >>> print p.as_ul() 2713 <li >Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>2714 <li >Password: <input type="password" name="password" /> Choose wisely.</li>2713 <li class="required">Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li> 2714 <li class="required">Password: <input type="password" name="password" /> Choose wisely.</li> 2715 2715 >>> print p.as_p() 2716 <p >Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</p>2717 <p >Password: <input type="password" name="password" /> Choose wisely.</p>2716 <p class="required">Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</p> 2717 <p class="required">Password: <input type="password" name="password" /> Choose wisely.</p> 2718 2718 >>> print p.as_table() 2719 <tr ><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br />e.g., user@example.com</td></tr>2720 <tr ><th>Password:</th><td><input type="password" name="password" /><br />Choose wisely.</td></tr>2719 <tr class="required"><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br />e.g., user@example.com</td></tr> 2720 <tr class="required"><th>Password:</th><td><input type="password" name="password" /><br />Choose wisely.</td></tr> 2721 2721 2722 2722 The help text is displayed whether or not data is provided for the form. 2723 2723 >>> p = UserRegistration({'username': u'foo'}, auto_id=False) 2724 2724 >>> print p.as_ul() 2725 <li >Username: <input type="text" name="username" value="foo" maxlength="10" /> e.g., user@example.com</li>2726 <li ><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> Choose wisely.</li>2725 <li class="required">Username: <input type="text" name="username" value="foo" maxlength="10" /> e.g., user@example.com</li> 2726 <li class="error required"><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> Choose wisely.</li> 2727 2727 2728 2728 help_text is not displayed for hidden fields. It can be used for documentation 2729 2729 purposes, though. … … 2733 2733 ... next = CharField(widget=HiddenInput, initial='/', help_text='Redirect destination') 2734 2734 >>> p = UserRegistration(auto_id=False) 2735 2735 >>> print p.as_ul() 2736 <li >Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>2737 <li >Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li>2736 <li class="required">Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li> 2737 <li class="required">Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li> 2738 2738 2739 2739 Help text can include arbitrary Unicode characters. 2740 2740 >>> class UserRegistration(Form): 2741 2741 ... username = CharField(max_length=10, help_text='ŠĐĆŽćžšđ') 2742 2742 >>> p = UserRegistration(auto_id=False) 2743 2743 >>> p.as_ul() 2744 u'<li >Username: <input type="text" name="username" maxlength="10" /> \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</li>'2744 u'<li class="required">Username: <input type="text" name="username" maxlength="10" /> \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</li>' 2745 2745 2746 2746 # Subclassing forms ########################################################### 2747 2747 … … 2756 2756 ... instrument = CharField() 2757 2757 >>> p = Person(auto_id=False) 2758 2758 >>> print p.as_ul() 2759 <li >First name: <input type="text" name="first_name" /></li>2760 <li >Last name: <input type="text" name="last_name" /></li>2761 <li >Birthday: <input type="text" name="birthday" /></li>2759 <li class="required">First name: <input type="text" name="first_name" /></li> 2760 <li class="required">Last name: <input type="text" name="last_name" /></li> 2761 <li class="required">Birthday: <input type="text" name="birthday" /></li> 2762 2762 >>> m = Musician(auto_id=False) 2763 2763 >>> print m.as_ul() 2764 <li >First name: <input type="text" name="first_name" /></li>2765 <li >Last name: <input type="text" name="last_name" /></li>2766 <li >Birthday: <input type="text" name="birthday" /></li>2767 <li >Instrument: <input type="text" name="instrument" /></li>2764 <li class="required">First name: <input type="text" name="first_name" /></li> 2765 <li class="required">Last name: <input type="text" name="last_name" /></li> 2766 <li class="required">Birthday: <input type="text" name="birthday" /></li> 2767 <li class="required">Instrument: <input type="text" name="instrument" /></li> 2768 2768 2769 2769 Yes, you can subclass multiple forms. The fields are added in the order in 2770 2770 which the parent classes are listed. … … 2778 2778 ... haircut_type = CharField() 2779 2779 >>> b = Beatle(auto_id=False) 2780 2780 >>> print b.as_ul() 2781 <li >First name: <input type="text" name="first_name" /></li>2782 <li >Last name: <input type="text" name="last_name" /></li>2783 <li >Birthday: <input type="text" name="birthday" /></li>2784 <li >Instrument: <input type="text" name="instrument" /></li>2785 <li >Haircut type: <input type="text" name="haircut_type" /></li>2781 <li class="required">First name: <input type="text" name="first_name" /></li> 2782 <li class="required">Last name: <input type="text" name="last_name" /></li> 2783 <li class="required">Birthday: <input type="text" name="birthday" /></li> 2784 <li class="required">Instrument: <input type="text" name="instrument" /></li> 2785 <li class="required">Haircut type: <input type="text" name="haircut_type" /></li> 2786 2786 2787 2787 # Forms with prefixes ######################################################### 2788 2788 … … 2804 2804 ... } 2805 2805 >>> p = Person(data, prefix='person1') 2806 2806 >>> print p.as_ul() 2807 <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>2808 <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>2809 <li ><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" /></li>2807 <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> 2808 <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> 2809 <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> 2810 2810 >>> print p['first_name'] 2811 2811 <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /> 2812 2812 >>> print p['last_name'] … … 2881 2881 ... return self.prefix and '%s-prefix-%s' % (self.prefix, field_name) or field_name 2882 2882 >>> p = Person(prefix='foo') 2883 2883 >>> print p.as_ul() 2884 <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>2885 <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>2886 <li ><label for="id_foo-prefix-birthday">Birthday:</label> <input type="text" name="foo-prefix-birthday" id="id_foo-prefix-birthday" /></li>2884 <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> 2885 <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> 2886 <li class="required"><label for="id_foo-prefix-birthday">Birthday:</label> <input type="text" name="foo-prefix-birthday" id="id_foo-prefix-birthday" /></li> 2887 2887 >>> data = { 2888 2888 ... 'foo-prefix-first_name': u'John', 2889 2889 ... 'foo-prefix-last_name': u'Lennon', … … 2971 2971 >>> print my_function('GET', {}) 2972 2972 <form action="" method="post"> 2973 2973 <table> 2974 <tr ><th>Username:</th><td><input type="text" name="username" maxlength="10" /></td></tr>2975 <tr ><th>Password1:</th><td><input type="password" name="password1" /></td></tr>2976 <tr ><th>Password2:</th><td><input type="password" name="password2" /></td></tr>2974 <tr class="required"><th>Username:</th><td><input type="text" name="username" maxlength="10" /></td></tr> 2975 <tr class="required"><th>Password1:</th><td><input type="password" name="password1" /></td></tr> 2976 <tr class="required"><th>Password2:</th><td><input type="password" name="password2" /></td></tr> 2977 2977 </table> 2978 2978 <input type="submit" /> 2979 2979 </form> … … 2983 2983 <form action="" method="post"> 2984 2984 <table> 2985 2985 <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr> 2986 <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>2987 <tr ><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr>2988 <tr ><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr>2986 <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> 2987 <tr class="required"><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr> 2988 <tr class="required"><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr> 2989 2989 </table> 2990 2990 <input type="submit" /> 2991 2991 </form>