Ticket #4232: 4232.diff

File 4232.diff, 4.2 KB (added by heckj@…, 17 years ago)

final update - cleaned up view per request and removed nasty tabs

  • docs/newforms.txt

     
    286286    ...
    287287    AttributeError: 'ContactForm' object has no attribute 'clean_data'
    288288
     289
     290Example View
     291~~~~~~~~~~~~
     292
     293Putting this all together, here is a simple view method for the example of our contact form:
     294
     295    from django.shortcuts import render_to_response
     296    from django import newforms as forms
     297
     298    class ContactForm(forms.Form):
     299        subject = forms.CharField(max_length=100)
     300        message = forms.CharField()
     301        sender = forms.EmailField()
     302        cc_myself = forms.BooleanField()
     303   
     304    def contact(request):
     305        if request.POST:
     306            f = ContactForm(request.POST)
     307            if f.is_valid:
     308                # ... do something with f.clean_data
     309        else:
     310            f = ContactForm()
     311        return render_to_response('contact.html', {'form': f})
     312
    289313Outputting forms as HTML
    290314------------------------
    291315
     
    362386    <p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>
    363387    <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>
    364388
     389In a template, you can invoke this if the form has been handed into the context. For example:
     390
     391    {{ f.as_p }}
     392
     393
    365394``as_ul()``
    366395~~~~~~~~~~~
    367396
     
    378407    <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>
    379408    <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>
    380409
     410In a template, you can invoke this if the form has been handed into the context. For example:
     411
     412    {{ f.as_ul }}
     413
    381414``as_table()``
    382415~~~~~~~~~~~~~~
    383416
     
    394427    <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>
    395428    <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>
    396429
     430In a template, you can invoke this if the form has been handed into the context. For example:
     431
     432    {{ f.as_table }}
     433
     434which is the same as
     435
     436    {{ f }}
     437   
     438
    397439Configuring HTML ``<label>`` tags
    398440~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    399441
     
    573615    >>> str(f['subject'].errors)
    574616    ''
    575617
     618In the templates
     619----------------
     620
     621With the above example, lets put this into a view and show how you can use these
     622parts from the template designer's point of view. Assuming you start with a view like:
     623
     624    def contact(request):
     625        form = ContactForm()
     626        if request.method == 'POST':
     627            new_data = request.POST.copy()
     628            form = ContactForm(new_data)
     629            if form.is_valid():
     630                # do form processing here...
     631        return render_to_response('contact.html', {'form': form})
     632
     633Then you can have a simple template that uses the shortcuts for form.as_ul, form.as_p, or form.as_table. An example ``contact.html`` template::
     634
     635    <form method="POST">
     636    {{ form }}
     637    </form>
     638
     639or
     640
     641    <form method="POST">
     642    {{ form.as_table }}
     643    </form>
     644
     645If you wanted to work with the individual inputs of the form, you can either call out the fields directly, or iterate over them:
     646
     647    <form method="POST">
     648    <dl>
     649    {% for field in form %}
     650        <dt>{{ field.label }}</dt>
     651        <dd>{{ field }}</dd>
     652        <dd>{{ field.help_text }}</dd>
     653        {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %}
     654    {% endfor %}
     655    </dl>
     656    </form>
     657
     658Alternately:
     659
     660    <form method="POST">
     661    <ul class="myformclass">
     662        <li>{{ form.sender.label }} {{ form.sender.label }}</li>
     663        <li class="helptext" >{{ form.sender.help_text }}</li>
     664        {% if form.sender.errors %}<ul class="errorlist">{{ form.sender.errors }}</dd>{% endif %}
     665
     666        <li>{{ form.subject.label }} {{ form.subject.label }}</li>
     667        <li class="helptext" >{{ form.subject.help_text }}</li>
     668        {% if form.subject.errors %}<ul class="errorlist">{{ form.subject.errors }}</dd>{% endif %}
     669
     670        ...
     671    </ul>
     672    </form>
     673
     674
    576675Subclassing forms
    577676-----------------
    578677
Back to Top