| | 289 | |
| | 290 | Example View |
| | 291 | ~~~~~~~~~~~~ |
| | 292 | |
| | 293 | Putting 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 | |
| | 618 | In the templates |
| | 619 | ---------------- |
| | 620 | |
| | 621 | With the above example, lets put this into a view and show how you can use these |
| | 622 | parts 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 | |
| | 633 | Then 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 | |
| | 639 | or |
| | 640 | |
| | 641 | <form method="POST"> |
| | 642 | {{ form.as_table }} |
| | 643 | </form> |
| | 644 | |
| | 645 | If 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 | |
| | 658 | Alternately: |
| | 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 | |