Changeset 5442
- Timestamp:
- 06/07/07 17:28:06 (1 year ago)
- Files:
-
- django/trunk/docs/newforms.txt (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/docs/newforms.txt
r5358 r5442 300 300 empty string, because ``nick_name`` is ``CharField``, and ``CharField``\s treat 301 301 empty values as an empty string. Each field type knows what its "blank" value 302 is -- e.g., for ``DateField``, it's ``None`` instead of the empty string. 302 is -- e.g., for ``DateField``, it's ``None`` instead of the empty string. For 303 full details on each field's behavior in this case, see the "Empty value" note 304 for each field in the "Built-in ``Field`` classes" section below. 303 305 304 306 Behavior of unbound forms 305 307 ~~~~~~~~~~~~~~~~~~~~~~~~~ 306 308 307 It's meaningless to request "clean " data in a form with no data, but, for the309 It's meaningless to request "cleaned" data in a form with no data, but, for the 308 310 record, here's what happens with unbound forms:: 309 311 … … 607 609 608 610 Let's put this all together and use the ``ContactForm`` example in a Django 609 view and template. This example view displays the contact form by default and 610 validates/processes it if accessed via a POST request:: 611 view and template. 612 613 Simple view example 614 ~~~~~~~~~~~~~~~~~~~ 615 616 This example view displays the contact form by default and validates/processes 617 it if accessed via a POST request:: 611 618 612 619 def contact(request): … … 620 627 return render_to_response('contact.html', {'form': form}) 621 628 622 Simple template output623 ~~~~~~~~~~~~~~~~~~~~~~ 624 625 The template , ``contact.html``, is responsible for displaying the form as HTML.626 To do this, we can use the techniques outlined in the "Outputting forms as HTML" 627 section above.629 Simple template example 630 ~~~~~~~~~~~~~~~~~~~~~~~ 631 632 The template in the above view example, ``contact.html``, is responsible for 633 displaying the form as HTML. To do this, we can use the techniques outlined in 634 the "Outputting forms as HTML" section above. 628 635 629 636 The simplest way to display a form's HTML is to use the variable on its own, … … 678 685 This iteration technique is useful if you want to apply the same HTML 679 686 formatting to each field, or if you don't know the names of the form fields 680 ahead of time. Note that the fields will be listedin the order in which687 ahead of time. Note that the fields will be iterated over in the order in which 681 688 they're defined in the ``Form`` class. 682 689 … … 702 709 ----------------- 703 710 704 If you subclass a custom ``Form`` class, the resulting ``Form`` class will 711 If you have multiple ``Form`` classes that share fields, you can use 712 subclassing to remove redundancy. 713 714 When you subclass a custom ``Form`` class, the resulting subclass will 705 715 include all fields of the parent class(es), followed by the fields you define 706 716 in the subclass. … … 1202 1212 mentioned above (``required``, ``label``, ``initial``, ``widget``, 1203 1213 ``help_text``). 1214 1215 A simple example 1216 ~~~~~~~~~~~~~~~~ 1217 1218 Here's a simple example of a custom field that validates its input is a string 1219 containing comma-separated e-mail addresses, with at least one address. We'll 1220 keep it simple and assume e-mail validation is contained in a function called 1221 ``is_valid_email()``. The full class:: 1222 1223 from django import newforms as forms 1224 1225 class MultiEmailField(forms.Field): 1226 def clean(self, value): 1227 emails = value.split(',') 1228 for email in emails: 1229 if not is_valid_email(email): 1230 raise forms.ValidationError('%s is not a valid e-mail address.' % email) 1231 if not emails: 1232 raise forms.ValidationError('Enter at least one e-mail address.') 1233 return emails 1234 1235 Let's alter the ongoing ``ContactForm`` example to demonstrate how you'd use 1236 this in a form. Simply use ``MultiEmailField`` instead of ``forms.EmailField``, 1237 like so:: 1238 1239 class ContactForm(forms.Form): 1240 subject = forms.CharField(max_length=100) 1241 message = forms.CharField() 1242 senders = MultiEmailField() 1243 cc_myself = forms.BooleanField() 1204 1244 1205 1245 Generating forms for models
