Ticket #4232: newforms.diff

File newforms.diff, 2.0 KB (added by heckj@…, 17 years ago)

documentation updates

  • newforms.txt

     
    573573    >>> str(f['subject'].errors)
    574574    ''
    575575
     576In the templates
     577----------------
     578
     579With the above example, lets put this into a view and show how you can use these
     580parts from the template designer's point of view. Assuming you start with a view like:
     581
     582    def contact(request):
     583        form = ContactForm()
     584                if request.POST:
     585                        new_data = request.POST.copy()
     586            form = ContactForm(new_data)
     587            if form.is_valid():
     588                # do form processing here...
     589        return render_to_response('contact.html', {'form': form})
     590
     591Then 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::
     592
     593    <form method="POST">
     594    {{ form }}
     595    </form>
     596
     597or
     598
     599    <form method="POST">
     600    {{ form.as_table }}
     601    </form>
     602
     603If you wanted to work with the individual inputs of the form, you can either call out the fields directly, or iterate over them:
     604
     605    <form method="POST">
     606    <dl>
     607    {% for field in form %}
     608        <dt>{{ field.label }}</dt>
     609        <dd>{{ field }}</dd>
     610        <dd>{{ field.help_text }}</dd>
     611        {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %}
     612    {% endfor %}
     613        </dl>
     614    </form>
     615
     616Alternately:
     617
     618        <form method="POST">
     619        <ul class="myformclass">
     620            <li>{{ form.sender.label }} {{ form.sender.label }}</li>
     621            <li class="helptext" >{{ form.sender.help_text }}</li>
     622            {% if form.sender.errors %}<ul class="errorlist">{{ form.sender.errors }}</dd>{% endif %}
     623
     624            <li>{{ form.subject.label }} {{ form.subject.label }}</li>
     625            <li class="helptext" >{{ form.subject.help_text }}</li>
     626            {% if form.subject.errors %}<ul class="errorlist">{{ form.subject.errors }}</dd>{% endif %}
     627
     628                ...
     629        </ul>
     630        </form>
     631
     632
    576633Subclassing forms
    577634-----------------
    578635
Back to Top