Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Edited docs/newforms.txt changes from [5294] and [5295]

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5298 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1bf43df33dda231daab66451a7efcbc6c68304ae 1 parent edc0fcd
Adrian Holovaty authored May 20, 2007

Showing 1 changed file with 52 additions and 73 deletions. Show diff stats Hide diff stats

  1. 125  docs/newforms.txt
125  docs/newforms.txt
@@ -313,33 +313,6 @@ record, here's what happens with unbound forms::
313 313
     ...
314 314
     AttributeError: 'ContactForm' object has no attribute 'cleaned_data'
315 315
 
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.http import HttpResponseRedirect
325  
-    from django import newforms as forms
326  
-
327  
-    class ContactForm(forms.Form):
328  
-        subject = forms.CharField(max_length=100)
329  
-        message = forms.CharField()
330  
-        sender = forms.EmailField()
331  
-        cc_myself = forms.BooleanField()
332  
-
333  
-    def contact(request):
334  
-        if request.POST:
335  
-            f = ContactForm(request.POST)
336  
-            if f.is_valid:
337  
-                # ... do something with f.cleaned_data
338  
-                return HttpResponseRedirect('/url/on_success/')
339  
-        else:
340  
-            f = ContactForm()
341  
-        return render_to_response('contact.html', {'form': f})
342  
-
343 316
 Outputting forms as HTML
344 317
 ------------------------
345 318
 
@@ -416,12 +389,6 @@ containing one field::
416 389
     <p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>
417 390
     <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>
418 391
 
419  
-In a template, you can invoke this if the form has been handed into the
420  
-context. For example::
421  
-
422  
-    {{ f.as_p }}
423  
-
424  
-
425 392
 ``as_ul()``
426 393
 ~~~~~~~~~~~
427 394
 
@@ -438,11 +405,6 @@ so that you can specify any HTML attributes on the ``<ul>`` for flexibility::
438 405
     <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>
439 406
     <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>
440 407
 
441  
-In a template, you can invoke this if the form has been handed into the
442  
-context. For example::
443  
-
444  
-    {{ f.as_ul }}
445  
-
446 408
 ``as_table()``
447 409
 ~~~~~~~~~~~~~~
448 410
 
@@ -459,18 +421,6 @@ calls its ``as_table()`` method behind the scenes::
459 421
     <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>
460 422
     <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>
461 423
 
462  
-In a template, you can invoke this if the form has been handed into the
463  
-context. For example::
464  
-
465  
-    {{ f.as_table }}
466  
-
467  
-which is the same as
468  
-
469  
-::
470  
-
471  
-    {{ f }}
472  
-
473  
-
474 424
 Configuring HTML ``<label>`` tags
475 425
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
476 426
 
@@ -652,68 +602,97 @@ when printed::
652 602
     >>> str(f['subject'].errors)
653 603
     ''
654 604
 
655  
-In the templates
656  
-----------------
  605
+Using forms in views and templates
  606
+----------------------------------
657 607
 
658  
-Using the above example, let's put this into a view and show how you can use
659  
-these parts from the template designer's point of view. Assuming you start
660  
-with a view like this::
  608
+Let's put this all together and use the ``ContactForm`` example in a Django
  609
+view and template. This example view displays the contact form by default and
  610
+validates/processes it if accessed via a POST request::
661 611
 
662 612
     def contact(request):
663 613
         if request.method == 'POST':
664 614
             form = ContactForm(request.POST)
665 615
             if form.is_valid():
666  
-                # do form processing here...
  616
+                # Do form processing here...
667 617
                 return HttpResponseRedirect('/url/on_success/')
668 618
         else:
669 619
             form = ContactForm()
670 620
         return render_to_response('contact.html', {'form': form})
671 621
 
672  
-...you can have a simple template that uses the shortcuts ``form.as_ul``,
673  
-``form.as_p``, or ``form.as_table`` (which is the default rendering method for
674  
-a form variable). An example ``contact.html`` template::
  622
+Simple template output
  623
+~~~~~~~~~~~~~~~~~~~~~~
  624
+
  625
+The template, ``contact.html``, is responsible for displaying the form as HTML.
  626
+To do this, we can use the techniques outlined in the "Outputting forms as HTML"
  627
+section above.
675 628
 
676  
-    <form method="POST">
677  
-    {{ form }}
  629
+The simplest way to display a form's HTML is to use the variable on its own,
  630
+like this::
  631
+
  632
+    <form method="post">
  633
+    <table>{{ form }}</table>
  634
+    <input type="submit" />
678 635
     </form>
679 636
 
680  
-Equivalently, you could write::
  637
+The above template code will display the form as an HTML table, using the
  638
+``form.as_table()`` method explained previously. This works because Django's
  639
+template system displays an object's ``__str__()`` value, and the ``Form``
  640
+class' ``__str__()`` method calls its ``as_table()`` method.
  641
+
  642
+The following is equivalent but a bit more explicit::
681 643
 
682  
-    <form method="POST">
683  
-    {{ form.as_table }}
  644
+    <form method="post">
  645
+    <table>{{ form.as_table }}</table>
  646
+    <input type="submit" />
684 647
     </form>
685 648
 
686  
-If you wanted to work with the individual inputs of the form, you can either
687  
-call out the fields directly or iterate over them::
  649
+``form.as_ul`` and ``form.as_p`` are also available, as you may expect.
  650
+
  651
+Note that in the above two examples, we included the ``<form>``, ``<table>``
  652
+``<input type="submit" />``, ``</table>`` and ``</form>`` tags. The form
  653
+convenience methods (``as_table()``, ``as_ul()`` and ``as_p()``) do not include
  654
+that HTML.
  655
+
  656
+Complex template output
  657
+~~~~~~~~~~~~~~~~~~~~~~~
688 658
 
689  
-    <form method="POST">
  659
+As we've stressed several times, the ``as_table()``, ``as_ul()`` and ``as_p()``
  660
+methods are just shortcuts for the common case. You can also work with the
  661
+individual fields for complete template control over the form's design.
  662
+
  663
+The easiest way is to iterate over the form's fields, with
  664
+``{% for field in form %}``. For example::
  665
+
  666
+    <form method="post">
690 667
     <dl>
691 668
     {% for field in form %}
692 669
         <dt>{{ field.label }}</dt>
693 670
         <dd>{{ field }}</dd>
694  
-        <dd>{{ field.help_text }}</dd>
  671
+        {% if field.help_text %}<dd>{{ field.help_text }}</dd>{% endif %}
695 672
         {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %}
696 673
     {% endfor %}
697 674
     </dl>
  675
+    <input type="submit" />
698 676
     </form>
699 677
 
700  
-Alternatively::
  678
+Alternatively, you can arrange the form's fields explicitly, by name. Do that
  679
+by accessing ``{{ form.fieldname }}``, where ``fieldname`` is the field's name.
  680
+For example::
701 681
 
702  
-    <form method="POST">
  682
+    <form method="post">
703 683
     <ul class="myformclass">
704 684
         <li>{{ form.sender.label }} {{ form.sender.label }}</li>
705  
-        <li class="helptext" >{{ form.sender.help_text }}</li>
  685
+        <li class="helptext">{{ form.sender.help_text }}</li>
706 686
         {% if form.sender.errors %}<ul class="errorlist">{{ form.sender.errors }}</dd>{% endif %}
707 687
 
708 688
         <li>{{ form.subject.label }} {{ form.subject.label }}</li>
709  
-        <li class="helptext" >{{ form.subject.help_text }}</li>
  689
+        <li class="helptext">{{ form.subject.help_text }}</li>
710 690
         {% if form.subject.errors %}<ul class="errorlist">{{ form.subject.errors }}</dd>{% endif %}
711 691
 
712 692
         ...
713 693
     </ul>
714 694
     </form>
715 695
 
716  
-
717 696
 Subclassing forms
718 697
 -----------------
719 698
 

0 notes on commit 1bf43df

Please sign in to comment.
Something went wrong with that request. Please try again.