Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4232 -- Added example usages to the newforms documentation. Th…

…anks, Joe

Heck.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5294 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0f424f50849930f92ab37638cfa1e34caac09877 1 parent d8db013
Malcolm Tredinnick authored May 19, 2007

Showing 1 changed file with 109 additions and 0 deletions. Show diff stats Hide diff stats

  1. 109  docs/newforms.txt
109  docs/newforms.txt
@@ -313,6 +313,31 @@ 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 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
+
316 341
 Outputting forms as HTML
317 342
 ------------------------
318 343
 
@@ -389,6 +414,12 @@ containing one field::
389 414
     <p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>
390 415
     <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>
391 416
 
  417
+In a template, you can invoke this if the form has been handed into the
  418
+context. For example::
  419
+
  420
+    {{ f.as_p }}
  421
+
  422
+
392 423
 ``as_ul()``
393 424
 ~~~~~~~~~~~
394 425
 
@@ -405,6 +436,11 @@ so that you can specify any HTML attributes on the ``<ul>`` for flexibility::
405 436
     <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>
406 437
     <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>
407 438
 
  439
+In a template, you can invoke this if the form has been handed into the
  440
+context. For example::
  441
+
  442
+    {{ f.as_ul }}
  443
+
408 444
 ``as_table()``
409 445
 ~~~~~~~~~~~~~~
410 446
 
@@ -421,6 +457,18 @@ calls its ``as_table()`` method behind the scenes::
421 457
     <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>
422 458
     <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>
423 459
 
  460
+In a template, you can invoke this if the form has been handed into the
  461
+context. For example::
  462
+
  463
+    {{ f.as_table }}
  464
+
  465
+which is the same as
  466
+
  467
+::
  468
+
  469
+    {{ f }}
  470
+
  471
+
424 472
 Configuring HTML ``<label>`` tags
425 473
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
426 474
 
@@ -602,6 +650,67 @@ when printed::
602 650
     >>> str(f['subject'].errors)
603 651
     ''
604 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
+
  713
+
605 714
 Subclassing forms
606 715
 -----------------
607 716
 

0 notes on commit 0f424f5

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