Django

Code

Changeset 5294

Show
Ignore:
Timestamp:
05/19/07 14:13:39 (2 years ago)
Author:
mtredinnick
Message:

Fixed #4232 -- Added example usages to the newforms documentation. Thanks, Joe
Heck.

Files:

Legend:

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

    r5237 r5294  
    314314    AttributeError: 'ContactForm' object has no attribute 'cleaned_data' 
    315315 
     316 
     317Example View 
     318~~~~~~~~~~~~ 
     319 
     320Putting this all together, here is a simple view method that uses our contact 
     321form:: 
     322 
     323    from django.shortcuts import render_to_response 
     324    from django import newforms as forms 
     325 
     326    class ContactForm(forms.Form): 
     327        subject = forms.CharField(max_length=100) 
     328        message = forms.CharField() 
     329        sender = forms.EmailField() 
     330        cc_myself = forms.BooleanField() 
     331 
     332    def contact(request): 
     333        if request.POST: 
     334            f = ContactForm(request.POST) 
     335            if f.is_valid: 
     336                # ... do something with f.cleaned_data 
     337        else: 
     338            f = ContactForm() 
     339        return render_to_response('contact.html', {'form': f}) 
     340 
    316341Outputting forms as HTML 
    317342------------------------ 
     
    390415    <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p> 
    391416 
     417In a template, you can invoke this if the form has been handed into the 
     418context. For example:: 
     419 
     420    {{ f.as_p }} 
     421 
     422 
    392423``as_ul()`` 
    393424~~~~~~~~~~~ 
     
    406437    <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li> 
    407438 
     439In a template, you can invoke this if the form has been handed into the 
     440context. For example:: 
     441 
     442    {{ f.as_ul }} 
     443 
    408444``as_table()`` 
    409445~~~~~~~~~~~~~~ 
     
    421457    <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr> 
    422458    <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr> 
     459 
     460In a template, you can invoke this if the form has been handed into the 
     461context. For example:: 
     462 
     463    {{ f.as_table }} 
     464 
     465which is the same as 
     466 
     467:: 
     468 
     469    {{ f }} 
     470 
    423471 
    424472Configuring HTML ``<label>`` tags 
     
    602650    >>> str(f['subject'].errors) 
    603651    '' 
     652 
     653In the templates 
     654---------------- 
     655 
     656Using the above example, let's put this into a view and show how you can use 
     657these parts from the template designer's point of view. Assuming you start 
     658with a view like this:: 
     659 
     660    def contact(request): 
     661        form = ContactForm() 
     662        if request.method == 'POST': 
     663            new_data = request.POST.copy() 
     664            form = ContactForm(new_data) 
     665            if form.is_valid(): 
     666                # do form processing here... 
     667        return render_to_response('contact.html', {'form': form}) 
     668 
     669...you can have a simple template that uses the shortcuts ``form.as_ul``, 
     670``form.as_p``, or ``form.as_table`` (which is the default rendering method for 
     671a form variable). An example ``contact.html`` template:: 
     672 
     673    <form method="POST"> 
     674    {{ form }} 
     675    </form> 
     676 
     677Equivalently, you could write:: 
     678 
     679    <form method="POST"> 
     680    {{ form.as_table }} 
     681    </form> 
     682 
     683If you wanted to work with the individual inputs of the form, you can either 
     684call out the fields directly or iterate over them:: 
     685 
     686    <form method="POST"> 
     687    <dl> 
     688    {% for field in form %} 
     689        <dt>{{ field.label }}</dt> 
     690        <dd>{{ field }}</dd> 
     691        <dd>{{ field.help_text }}</dd> 
     692        {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %} 
     693    {% endfor %} 
     694    </dl> 
     695    </form> 
     696 
     697Alternatively:: 
     698 
     699    <form method="POST"> 
     700    <ul class="myformclass"> 
     701        <li>{{ form.sender.label }} {{ form.sender.label }}</li> 
     702        <li class="helptext" >{{ form.sender.help_text }}</li> 
     703        {% if form.sender.errors %}<ul class="errorlist">{{ form.sender.errors }}</dd>{% endif %} 
     704 
     705        <li>{{ form.subject.label }} {{ form.subject.label }}</li> 
     706        <li class="helptext" >{{ form.subject.help_text }}</li> 
     707        {% if form.subject.errors %}<ul class="errorlist">{{ form.subject.errors }}</dd>{% endif %} 
     708 
     709        ... 
     710    </ul> 
     711    </form> 
     712 
    604713 
    605714Subclassing forms