Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3107 -- newforms: Added Form.as_p()

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4178 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7e269bd390c9c4963bdce6522a8fafe6a618a5b1 1 parent 44add11
Adrian Holovaty authored December 07, 2006
27  django/newforms/forms.py
@@ -124,6 +124,33 @@ def as_ul(self):
124 124
                 output.append(str_hidden)
125 125
         return u'\n'.join(output)
126 126
 
  127
+    def as_p(self):
  128
+        "Returns this form rendered as HTML <p>s."
  129
+        top_errors = self.non_field_errors()
  130
+        output, hidden_fields = [], []
  131
+        for name, field in self.fields.items():
  132
+            bf = BoundField(self, field, name)
  133
+            bf_errors = bf.errors # Cache in local variable.
  134
+            if bf.is_hidden:
  135
+                if bf_errors:
  136
+                    top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
  137
+                hidden_fields.append(unicode(bf))
  138
+            else:
  139
+                if bf_errors:
  140
+                    output.append(u'<p>%s</p>' % bf_errors)
  141
+                output.append(u'<p>%s %s</p>' % (bf.label_tag(escape(bf.verbose_name+':')), bf))
  142
+        if top_errors:
  143
+            output.insert(0, u'<p>%s</p>' % top_errors)
  144
+        if hidden_fields: # Insert any hidden fields in the last <p>.
  145
+            str_hidden = u''.join(hidden_fields)
  146
+            if output:
  147
+                last_td = output[-1]
  148
+                # Chop off the trailing '</p>' and insert the hidden fields.
  149
+                output[-1] = last_td[:-4] + str_hidden + '</p>'
  150
+            else: # If there aren't any '<p>'s in the output, just append the hidden fields.
  151
+                output.append(str_hidden)
  152
+        return u'\n'.join(output)
  153
+
127 154
     def non_field_errors(self):
128 155
         """
129 156
         Returns an ErrorList of errors that aren't associated with a particular
52  tests/regressiontests/forms/tests.py
@@ -1368,6 +1368,13 @@
1368 1368
 <li><ul class="errorlist"><li>This field is required.</li></ul>First name: <input type="text" name="first_name" /></li>
1369 1369
 <li><ul class="errorlist"><li>This field is required.</li></ul>Last name: <input type="text" name="last_name" /></li>
1370 1370
 <li><ul class="errorlist"><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li>
  1371
+>>> print p.as_p()
  1372
+<p><ul class="errorlist"><li>This field is required.</li></ul></p>
  1373
+<p>First name: <input type="text" name="first_name" /></p>
  1374
+<p><ul class="errorlist"><li>This field is required.</li></ul></p>
  1375
+<p>Last name: <input type="text" name="last_name" /></p>
  1376
+<p><ul class="errorlist"><li>This field is required.</li></ul></p>
  1377
+<p>Birthday: <input type="text" name="birthday" /></p>
1371 1378
 
1372 1379
 If you don't pass any values to the Form's __init__(), or if you pass None,
1373 1380
 the Form won't do any validation. Form.errors will be an empty dictionary *but*
@@ -1389,6 +1396,10 @@
1389 1396
 <li>First name: <input type="text" name="first_name" /></li>
1390 1397
 <li>Last name: <input type="text" name="last_name" /></li>
1391 1398
 <li>Birthday: <input type="text" name="birthday" /></li>
  1399
+>>> print p.as_p()
  1400
+<p>First name: <input type="text" name="first_name" /></p>
  1401
+<p>Last name: <input type="text" name="last_name" /></p>
  1402
+<p>Birthday: <input type="text" name="birthday" /></p>
1392 1403
 
1393 1404
 Unicode values are handled properly.
1394 1405
 >>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'})
@@ -1396,6 +1407,8 @@
1396 1407
 u'<tr><td>First name:</td><td><input type="text" name="first_name" value="John" /></td></tr>\n<tr><td>Last name:</td><td><input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /></td></tr>\n<tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr>'
1397 1408
 >>> p.as_ul()
1398 1409
 u'<li>First name: <input type="text" name="first_name" value="John" /></li>\n<li>Last name: <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /></li>\n<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li>'
  1410
+>>> p.as_p()
  1411
+u'<p>First name: <input type="text" name="first_name" value="John" /></p>\n<p>Last name: <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /></p>\n<p>Birthday: <input type="text" name="birthday" value="1940-10-9" /></p>'
1399 1412
 
1400 1413
 >>> p = Person({'last_name': u'Lennon'})
1401 1414
 >>> p.errors
@@ -1432,14 +1445,18 @@
1432 1445
 into which the field's name will be inserted. It will also put a <label> around
1433 1446
 the human-readable labels for a field.
1434 1447
 >>> p = Person(auto_id='id_%s')
1435  
->>> print p.as_ul()
1436  
-<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
1437  
-<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
1438  
-<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>
1439 1448
 >>> print p.as_table()
1440 1449
 <tr><td><label for="id_first_name">First name:</label></td><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
1441 1450
 <tr><td><label for="id_last_name">Last name:</label></td><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
1442 1451
 <tr><td><label for="id_birthday">Birthday:</label></td><td><input type="text" name="birthday" id="id_birthday" /></td></tr>
  1452
+>>> print p.as_ul()
  1453
+<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
  1454
+<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
  1455
+<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>
  1456
+>>> print p.as_p()
  1457
+<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
  1458
+<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
  1459
+<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
1443 1460
 
1444 1461
 If auto_id is any True value whose str() does not contain '%s', the "id"
1445 1462
 attribute will be the name of the field.
@@ -1596,6 +1613,12 @@
1596 1613
 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
1597 1614
 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
1598 1615
 </ul></li>
  1616
+>>> print f.as_p()
  1617
+<p><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
  1618
+<p><label for="id_language_0">Language:</label> <ul>
  1619
+<li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
  1620
+<li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
  1621
+</ul></p>
1599 1622
 
1600 1623
 MultipleChoiceField is a special case, as its data is required to be a list:
1601 1624
 >>> class SongForm(Form):
@@ -1754,9 +1777,9 @@
1754 1777
 <tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
1755 1778
 <tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
1756 1779
 
1757  
-HiddenInput widgets are displayed differently in the as_table() and as_ul()
1758  
-output of a Form -- their verbose names are not displayed, and a separate
1759  
-<tr>/<li> is not displayed. They're displayed in the last <td>/<li> of the
  1780
+HiddenInput widgets are displayed differently in the as_table(), as_ul()
  1781
+and as_p() output of a Form -- their verbose names are not displayed, and a
  1782
+separate row is not displayed. They're displayed in the last row of the
1760 1783
 form, directly after that row's form element.
1761 1784
 >>> class Person(Form):
1762 1785
 ...     first_name = CharField()
@@ -1772,6 +1795,10 @@
1772 1795
 <li>First name: <input type="text" name="first_name" /></li>
1773 1796
 <li>Last name: <input type="text" name="last_name" /></li>
1774 1797
 <li>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></li>
  1798
+>>> print p.as_p()
  1799
+<p>First name: <input type="text" name="first_name" /></p>
  1800
+<p>Last name: <input type="text" name="last_name" /></p>
  1801
+<p>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p>
1775 1802
 
1776 1803
 With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
1777 1804
 >>> p = Person(auto_id='id_%s')
@@ -1783,6 +1810,10 @@
1783 1810
 <li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
1784 1811
 <li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
1785 1812
 <li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></li>
  1813
+>>> print p.as_p()
  1814
+<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
  1815
+<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
  1816
+<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>
1786 1817
 
1787 1818
 If a field with a HiddenInput has errors, the as_table() and as_ul() output
1788 1819
 will include the error message(s) with the text "(Hidden field [fieldname]) "
@@ -1799,6 +1830,11 @@
1799 1830
 <li>First name: <input type="text" name="first_name" value="John" /></li>
1800 1831
 <li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
1801 1832
 <li>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li>
  1833
+>>> print p.as_p()
  1834
+<p><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></p>
  1835
+<p>First name: <input type="text" name="first_name" value="John" /></p>
  1836
+<p>Last name: <input type="text" name="last_name" value="Lennon" /></p>
  1837
+<p>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>
1802 1838
 
1803 1839
 A corner case: It's possible for a form to have only HiddenInputs.
1804 1840
 >>> class TestForm(Form):
@@ -1809,6 +1845,8 @@
1809 1845
 <input type="hidden" name="foo" /><input type="hidden" name="bar" />
1810 1846
 >>> print p.as_ul()
1811 1847
 <input type="hidden" name="foo" /><input type="hidden" name="bar" />
  1848
+>>> print p.as_p()
  1849
+<input type="hidden" name="foo" /><input type="hidden" name="bar" />
1812 1850
 
1813 1851
 A Form's fields are displayed in the same order in which they were defined.
1814 1852
 >>> class TestForm(Form):

0 notes on commit 7e269bd

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