Code

Ticket #4232: newforms.2.diff

File newforms.2.diff, 4.1 KB (added by heckj@…, 7 years ago)

documentation updates v2

Line 
1Index: docs/newforms.txt
2===================================================================
3--- docs/newforms.txt   (revision 5174)
4+++ docs/newforms.txt   (working copy)
5@@ -286,6 +286,30 @@
6     ...
7     AttributeError: 'ContactForm' object has no attribute 'clean_data'
8 
9+
10+Example View
11+~~~~~~~~~~~~
12+
13+Putting this all together, here is a simple view method for the example of our contact form:
14+
15+       from django.shortcuts import render_to_response
16+       from django import newforms as forms
17+
18+    class ContactForm(forms.Form):
19+        subject = forms.CharField(max_length=100)
20+        message = forms.CharField()
21+        sender = forms.EmailField()
22+        cc_myself = forms.BooleanField()
23+       
24+    def contact(request):
25+               if request.POST:
26+                       f = ContactForm(request.POST)
27+                       if f.is_valid:
28+                               # ... do something with f.clean_data
29+               else:
30+                       f = ContactForm()
31+               return render_to_response('contact.html', {'form': f})
32+
33 Outputting forms as HTML
34 ------------------------
35 
36@@ -362,6 +386,11 @@
37     <p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></p>
38     <p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>
39 
40+In a template, you can invoke this if the form has been handed into the context. For example:
41+
42+    {{ f.as_p }}
43+
44+
45 ``as_ul()``
46 ~~~~~~~~~~~
47 
48@@ -378,6 +407,10 @@
49     <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>
50     <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>
51 
52+In a template, you can invoke this if the form has been handed into the context. For example:
53+
54+    {{ f.as_ul }}
55+
56 ``as_table()``
57 ~~~~~~~~~~~~~~
58 
59@@ -394,6 +427,15 @@
60     <tr><th><label for="id_sender">Sender:</label></th><td><input type="text" name="sender" id="id_sender" /></td></tr>
61     <tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" /></td></tr>
62 
63+In a template, you can invoke this if the form has been handed into the context. For example:
64+
65+    {{ f.as_table }}
66+
67+which is the same as
68+
69+       {{ f }}
70+       
71+
72 Configuring HTML ``<label>`` tags
73 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74 
75@@ -573,6 +615,63 @@
76     >>> str(f['subject'].errors)
77     ''
78 
79+In the templates
80+----------------
81+
82+With the above example, lets put this into a view and show how you can use these
83+parts from the template designer's point of view. Assuming you start with a view like:
84+
85+    def contact(request):
86+        form = ContactForm()
87+               if request.POST:
88+                       new_data = request.POST.copy()
89+            form = ContactForm(new_data)
90+            if form.is_valid():
91+                # do form processing here...
92+        return render_to_response('contact.html', {'form': form})
93+
94+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::
95+
96+    <form method="POST">
97+    {{ form }}
98+    </form>
99+
100+or
101+
102+    <form method="POST">
103+    {{ form.as_table }}
104+    </form>
105+
106+If you wanted to work with the individual inputs of the form, you can either call out the fields directly, or iterate over them:
107+
108+    <form method="POST">
109+    <dl>
110+    {% for field in form %}
111+        <dt>{{ field.label }}</dt>
112+        <dd>{{ field }}</dd>
113+        <dd>{{ field.help_text }}</dd>
114+        {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %}
115+    {% endfor %}
116+       </dl>
117+    </form>
118+
119+Alternately:
120+
121+       <form method="POST">
122+       <ul class="myformclass">
123+           <li>{{ form.sender.label }} {{ form.sender.label }}</li>
124+           <li class="helptext" >{{ form.sender.help_text }}</li>
125+           {% if form.sender.errors %}<ul class="errorlist">{{ form.sender.errors }}</dd>{% endif %}
126+
127+           <li>{{ form.subject.label }} {{ form.subject.label }}</li>
128+           <li class="helptext" >{{ form.subject.help_text }}</li>
129+           {% if form.subject.errors %}<ul class="errorlist">{{ form.subject.errors }}</dd>{% endif %}
130+
131+               ...
132+       </ul>
133+       </form>
134+
135+
136 Subclassing forms
137 -----------------
138