Code

Ticket #3286: tests2.py.diff

File tests2.py.diff, 10.0 KB (added by Jannis <jl@…>, 7 years ago)

revision of tests and docs for new as_div()

Line 
1Index: tests/regressiontests/forms/tests.py
2===================================================================
3--- tests/regressiontests/forms/tests.py        (Revision 4306)
4+++ tests/regressiontests/forms/tests.py        (Arbeitskopie)
5@@ -1575,6 +1575,13 @@
6 <p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
7 <p><ul class="errorlist"><li>This field is required.</li></ul></p>
8 <p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
9+>>> print p.as_div()
10+<div><ul class="errorlist"><li>This field is required.</li></ul></div>
11+<div><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></div>
12+<div><ul class="errorlist"><li>This field is required.</li></ul></div>
13+<div><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></div>
14+<div><ul class="errorlist"><li>This field is required.</li></ul></div>
15+<div><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></div>
16 
17 If you don't pass any values to the Form's __init__(), or if you pass None,
18 the Form will be considered unbound and won't do any validation. Form.errors
19@@ -1606,6 +1613,10 @@
20 <p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
21 <p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
22 <p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
23+>>> print p.as_div()
24+<div><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></div>
25+<div><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></div>
26+<div><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></div>
27 
28 Unicode values are handled properly.
29 >>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'})
30@@ -1615,6 +1626,8 @@
31 u'<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></li>\n<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></li>\n<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></li>'
32 >>> p.as_p()
33 u'<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></p>\n<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></p>\n<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></p>'
34+>>> p.as_div()
35+u'<div><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></div>\n<div><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></div>\n<div><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></div>'
36 
37 >>> p = Person({'last_name': u'Lennon'})
38 >>> p.errors
39@@ -1675,6 +1688,10 @@
40 <p><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></p>
41 <p><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></p>
42 <p><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></p>
43+>>> print p.as_div()
44+<div><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></div>
45+<div><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></div>
46+<div><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></div>
47 
48 If auto_id is any True value whose str() does not contain '%s', the "id"
49 attribute will be the name of the field.
50@@ -1837,6 +1854,12 @@
51 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
52 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
53 </ul></p>
54+>>> print f.as_div()
55+<div><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></div>
56+<div><label for="id_language_0">Language:</label> <ul>
57+<li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
58+<li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
59+</ul></div>
60 
61 MultipleChoiceField is a special case, as its data is required to be a list:
62 >>> class SongForm(Form):
63@@ -2022,9 +2045,9 @@
64 <tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>
65 <tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>
66 
67-HiddenInput widgets are displayed differently in the as_table(), as_ul()
68-and as_p() output of a Form -- their verbose names are not displayed, and a
69-separate row is not displayed. They're displayed in the last row of the
70+HiddenInput widgets are displayed differently in the as_table(), as_ul(),
71+as_p() and as_div() output of a Form -- their verbose names are not displayed,
72+and a separate row is not displayed. They're displayed in the last row of the
73 form, directly after that row's form element.
74 >>> class Person(Form):
75 ...     first_name = CharField()
76@@ -2044,6 +2067,10 @@
77 <p>First name: <input type="text" name="first_name" /></p>
78 <p>Last name: <input type="text" name="last_name" /></p>
79 <p>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p>
80+>>> print p.as_div()
81+<div>First name: <input type="text" name="first_name" /></div>
82+<div>Last name: <input type="text" name="last_name" /></div>
83+<div>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></div>
84 
85 With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
86 >>> p = Person(auto_id='id_%s')
87@@ -2059,6 +2086,10 @@
88 <p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
89 <p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
90 <p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></p>
91+>>> print p.as_div()
92+<div><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></div>
93+<div><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></div>
94+<div><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></div>
95 
96 If a field with a HiddenInput has errors, the as_table() and as_ul() output
97 will include the error message(s) with the text "(Hidden field [fieldname]) "
98@@ -2080,7 +2111,13 @@
99 <p>First name: <input type="text" name="first_name" value="John" /></p>
100 <p>Last name: <input type="text" name="last_name" value="Lennon" /></p>
101 <p>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>
102+>>> print p.as_div()
103+<div><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></div>
104+<div>First name: <input type="text" name="first_name" value="John" /></div>
105+<div>Last name: <input type="text" name="last_name" value="Lennon" /></div>
106+<div>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></div>
107 
108+
109 A corner case: It's possible for a form to have only HiddenInputs.
110 >>> class TestForm(Form):
111 ...     foo = CharField(widget=HiddenInput)
112@@ -2092,6 +2129,8 @@
113 <input type="hidden" name="foo" /><input type="hidden" name="bar" />
114 >>> print p.as_p()
115 <input type="hidden" name="foo" /><input type="hidden" name="bar" />
116+>>> print p.as_div()
117+<input type="hidden" name="foo" /><input type="hidden" name="bar" />
118 
119 A Form's fields are displayed in the same order in which they were defined.
120 >>> class TestForm(Form):
121Index: docs/newforms.txt
122===================================================================
123--- docs/newforms.txt   (Revision 4306)
124+++ docs/newforms.txt   (Arbeitskopie)
125@@ -376,6 +376,21 @@
126     <li><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></li>
127     <li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></li>
128 
129+``as_div()``
130+~~~~~~~~~~~
131+
132+``Form.as_div()`` renders the form as a series of ``<div>`` tags, with each ``<div>``
133+containing one field::
134+
135+    >>> f = ContactForm()
136+    >>> f.as_div()
137+    u'<div><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></div>\n<div><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></div>\n<div><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></div>\n<div><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></div>'
138+    >>> print f.as_div()
139+    <div><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" /></div>
140+    <div><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" /></div>
141+    <div><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" /></div>
142+    <div><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself" /></div>
143+
144 ``as_table()``
145 ~~~~~~~~~~~~~~