Django

Code

Changeset 5442

Show
Ignore:
Timestamp:
06/07/07 17:28:06 (1 year ago)
Author:
adrian
Message:

Made some changes to docs/newforms.txt that I'd had lying around

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/newforms.txt

    r5358 r5442  
    300300empty string, because ``nick_name`` is ``CharField``, and ``CharField``\s treat 
    301301empty 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. 
     302is -- e.g., for ``DateField``, it's ``None`` instead of the empty string. For 
     303full details on each field's behavior in this case, see the "Empty value" note 
     304for each field in the "Built-in ``Field`` classes" section below. 
    303305 
    304306Behavior of unbound forms 
    305307~~~~~~~~~~~~~~~~~~~~~~~~~ 
    306308 
    307 It's meaningless to request "clean" data in a form with no data, but, for the 
     309It's meaningless to request "cleaned" data in a form with no data, but, for the 
    308310record, here's what happens with unbound forms:: 
    309311 
     
    607609 
    608610Let'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:: 
     611view and template. 
     612 
     613Simple view example 
     614~~~~~~~~~~~~~~~~~~~ 
     615 
     616This example view displays the contact form by default and validates/processes 
     617it if accessed via a POST request:: 
    611618 
    612619    def contact(request): 
     
    620627        return render_to_response('contact.html', {'form': form}) 
    621628 
    622 Simple template output 
    623 ~~~~~~~~~~~~~~~~~~~~~~ 
    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. 
     629Simple template example 
     630~~~~~~~~~~~~~~~~~~~~~~~ 
     631 
     632The template in the above view example, ``contact.html``, is responsible for 
     633displaying the form as HTML. To do this, we can use the techniques outlined in 
     634the "Outputting forms as HTML" section above. 
    628635 
    629636The simplest way to display a form's HTML is to use the variable on its own, 
     
    678685This iteration technique is useful if you want to apply the same HTML 
    679686formatting 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 listed in the order in which 
     687ahead of time. Note that the fields will be iterated over in the order in which 
    681688they're defined in the ``Form`` class. 
    682689 
     
    702709----------------- 
    703710 
    704 If you subclass a custom ``Form`` class, the resulting ``Form`` class will 
     711If you have multiple ``Form`` classes that share fields, you can use 
     712subclassing to remove redundancy. 
     713 
     714When you subclass a custom ``Form`` class, the resulting subclass will 
    705715include all fields of the parent class(es), followed by the fields you define 
    706716in the subclass. 
     
    12021212mentioned above (``required``, ``label``, ``initial``, ``widget``, 
    12031213``help_text``). 
     1214 
     1215A simple example 
     1216~~~~~~~~~~~~~~~~ 
     1217 
     1218Here's a simple example of a custom field that validates its input is a string 
     1219containing comma-separated e-mail addresses, with at least one address. We'll 
     1220keep 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 
     1235Let's alter the ongoing ``ContactForm`` example to demonstrate how you'd use 
     1236this in a form. Simply use ``MultiEmailField`` instead of ``forms.EmailField``, 
     1237like 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() 
    12041244 
    12051245Generating forms for models