| | 316 | |
|---|
| | 317 | Example View |
|---|
| | 318 | ~~~~~~~~~~~~ |
|---|
| | 319 | |
|---|
| | 320 | Putting this all together, here is a simple view method that uses our contact |
|---|
| | 321 | form:: |
|---|
| | 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 | |
|---|
| | 652 | |
|---|
| | 653 | In the templates |
|---|
| | 654 | ---------------- |
|---|
| | 655 | |
|---|
| | 656 | Using the above example, let's put this into a view and show how you can use |
|---|
| | 657 | these parts from the template designer's point of view. Assuming you start |
|---|
| | 658 | with 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 |
|---|
| | 671 | a form variable). An example ``contact.html`` template:: |
|---|
| | 672 | |
|---|
| | 673 | <form method="POST"> |
|---|
| | 674 | {{ form }} |
|---|
| | 675 | </form> |
|---|
| | 676 | |
|---|
| | 677 | Equivalently, you could write:: |
|---|
| | 678 | |
|---|
| | 679 | <form method="POST"> |
|---|
| | 680 | {{ form.as_table }} |
|---|
| | 681 | </form> |
|---|
| | 682 | |
|---|
| | 683 | If you wanted to work with the individual inputs of the form, you can either |
|---|
| | 684 | call 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 | |
|---|
| | 697 | Alternatively:: |
|---|
| | 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 | |
|---|