~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/forms/forms.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
>>> import datetime
6
6
>>> import time
7
7
>>> import re
8
 
>>> try:
9
 
...     from decimal import Decimal
10
 
... except ImportError:
11
 
...     from django.utils._decimal import Decimal
 
8
>>> from decimal import Decimal
12
9
 
13
10
#########
14
11
# Forms #
295
292
>>> print f['get_spam']
296
293
<input checked="checked" type="checkbox" name="get_spam" />
297
294
 
 
295
'True' or 'true' should be rendered without a value attribute
 
296
>>> f = SignupForm({'email': 'test@example.com', 'get_spam': 'True'}, auto_id=False)
 
297
>>> print f['get_spam']
 
298
<input checked="checked" type="checkbox" name="get_spam" />
 
299
 
 
300
>>> f = SignupForm({'email': 'test@example.com', 'get_spam': 'true'}, auto_id=False)
 
301
>>> print f['get_spam']
 
302
<input checked="checked" type="checkbox" name="get_spam" />
 
303
 
 
304
A value of 'False' or 'false' should be rendered unchecked
 
305
>>> f = SignupForm({'email': 'test@example.com', 'get_spam': 'False'}, auto_id=False)
 
306
>>> print f['get_spam']
 
307
<input type="checkbox" name="get_spam" />
 
308
 
 
309
>>> f = SignupForm({'email': 'test@example.com', 'get_spam': 'false'}, auto_id=False)
 
310
>>> print f['get_spam']
 
311
<input type="checkbox" name="get_spam" />
 
312
 
298
313
Any Field can have a Widget class passed to its constructor:
299
314
>>> class ContactForm(Form):
300
315
...     subject = CharField()
1807
1822
>>> [f.name for f in form.visible_fields()]
1808
1823
['artist', 'name']
1809
1824
 
 
1825
# Hidden initial input gets its own unique id ################################
 
1826
 
 
1827
>>> class MyForm(Form):
 
1828
...     field1 = CharField(max_length=50, show_hidden_initial=True)
 
1829
>>> print MyForm()
 
1830
<tr><th><label for="id_field1">Field1:</label></th><td><input id="id_field1" type="text" name="field1" maxlength="50" /><input type="hidden" name="initial-field1" id="initial-id_field1" /></td></tr>
 
1831
 
 
1832
# The error_html_class and required_html_class attributes ####################
 
1833
 
 
1834
>>> class Person(Form):
 
1835
...     name = CharField()
 
1836
...     is_cool = NullBooleanField()
 
1837
...     email = EmailField(required=False)
 
1838
...     age = IntegerField()
 
1839
 
 
1840
>>> p = Person({})
 
1841
>>> p.error_css_class = 'error'
 
1842
>>> p.required_css_class = 'required'
 
1843
 
 
1844
>>> print p.as_ul()
 
1845
<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>
 
1846
<li class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool">
 
1847
<option value="1" selected="selected">Unknown</option>
 
1848
<option value="2">Yes</option>
 
1849
<option value="3">No</option>
 
1850
</select></li>
 
1851
<li><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></li>
 
1852
<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></li>
 
1853
 
 
1854
>>> print p.as_p()
 
1855
<ul class="errorlist"><li>This field is required.</li></ul>
 
1856
<p class="required error"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
 
1857
<p class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool">
 
1858
<option value="1" selected="selected">Unknown</option>
 
1859
<option value="2">Yes</option>
 
1860
<option value="3">No</option>
 
1861
</select></p>
 
1862
<p><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></p>
 
1863
<ul class="errorlist"><li>This field is required.</li></ul>
 
1864
<p class="required error"><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>
 
1865
 
 
1866
>>> print p.as_table()
 
1867
<tr class="required error"><th><label for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="name" id="id_name" /></td></tr>
 
1868
<tr class="required"><th><label for="id_is_cool">Is cool:</label></th><td><select name="is_cool" id="id_is_cool">
 
1869
<option value="1" selected="selected">Unknown</option>
 
1870
<option value="2">Yes</option>
 
1871
<option value="3">No</option>
 
1872
</select></td></tr>
 
1873
<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
 
1874
<tr class="required error"><th><label for="id_age">Age:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="age" id="id_age" /></td></tr>
 
1875
 
 
1876
 
 
1877
 
 
1878
# Checking that the label for SplitDateTimeField is not being displayed #####
 
1879
 
 
1880
>>> class EventForm(Form):
 
1881
...     happened_at = SplitDateTimeField(widget=widgets.SplitHiddenDateTimeWidget)
 
1882
...
 
1883
>>> form = EventForm()
 
1884
>>> form.as_ul()
 
1885
u'<input type="hidden" name="happened_at_0" id="id_happened_at_0" /><input type="hidden" name="happened_at_1" id="id_happened_at_1" />'
 
1886
 
1810
1887
"""