295
292
>>> print f['get_spam']
296
293
<input checked="checked" type="checkbox" name="get_spam" />
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" />
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" />
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" />
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" />
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']
1825
# Hidden initial input gets its own unique id ################################
1827
>>> class MyForm(Form):
1828
... field1 = CharField(max_length=50, show_hidden_initial=True)
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>
1832
# The error_html_class and required_html_class attributes ####################
1834
>>> class Person(Form):
1835
... name = CharField()
1836
... is_cool = NullBooleanField()
1837
... email = EmailField(required=False)
1838
... age = IntegerField()
1841
>>> p.error_css_class = 'error'
1842
>>> p.required_css_class = 'required'
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>
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>
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>
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>
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>
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>
1878
# Checking that the label for SplitDateTimeField is not being displayed #####
1880
>>> class EventForm(Form):
1881
... happened_at = SplitDateTimeField(widget=widgets.SplitHiddenDateTimeWidget)
1883
>>> form = EventForm()
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" />'