PageRenderTime 262ms CodeModel.GetById 80ms app.highlight 138ms RepoModel.GetById 23ms app.codeStats 2ms

/tests/regressiontests/forms/tests/forms.py

https://code.google.com/p/mango-py/
Python | 1724 lines | 1683 code | 16 blank | 25 comment | 10 complexity | 17beefea3cf04a0dedfb41b9e743e8a2 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1# -*- coding: utf-8 -*-
  2import datetime
  3from decimal import Decimal
  4import re
  5import time
  6from django.core.files.uploadedfile import SimpleUploadedFile
  7from django.forms import *
  8from django.http import QueryDict
  9from django.template import Template, Context
 10from django.utils.datastructures import MultiValueDict, MergeDict
 11from django.utils.safestring import mark_safe
 12from django.utils.unittest import TestCase
 13
 14
 15class Person(Form):
 16    first_name = CharField()
 17    last_name = CharField()
 18    birthday = DateField()
 19
 20
 21class PersonNew(Form):
 22    first_name = CharField(widget=TextInput(attrs={'id': 'first_name_id'}))
 23    last_name = CharField()
 24    birthday = DateField()
 25
 26
 27class FormsTestCase(TestCase):
 28    # A Form is a collection of Fields. It knows how to validate a set of data and it
 29    # knows how to render itself in a couple of default ways (e.g., an HTML table).
 30    # You can pass it data in __init__(), as a dictionary.
 31
 32    def test_form(self):
 33        # Pass a dictionary to a Form's __init__().
 34        p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'})
 35
 36        self.assertTrue(p.is_bound)
 37        self.assertEqual(p.errors, {})
 38        self.assertTrue(p.is_valid())
 39        self.assertEqual(p.errors.as_ul(), u'')
 40        self.assertEqual(p.errors.as_text(), u'')
 41        self.assertEqual(p.cleaned_data["first_name"], u'John')
 42        self.assertEqual(p.cleaned_data["last_name"], u'Lennon')
 43        self.assertEqual(p.cleaned_data["birthday"], datetime.date(1940, 10, 9))
 44        self.assertEqual(str(p['first_name']), '<input type="text" name="first_name" value="John" id="id_first_name" />')
 45        self.assertEqual(str(p['last_name']), '<input type="text" name="last_name" value="Lennon" id="id_last_name" />')
 46        self.assertEqual(str(p['birthday']), '<input type="text" name="birthday" value="1940-10-9" id="id_birthday" />')
 47        try:
 48            p['nonexistentfield']
 49            self.fail('Attempts to access non-existent fields should fail.')
 50        except KeyError:
 51            pass
 52
 53        form_output = []
 54
 55        for boundfield in p:
 56            form_output.append(str(boundfield))
 57
 58        self.assertEqual('\n'.join(form_output), """<input type="text" name="first_name" value="John" id="id_first_name" />
 59<input type="text" name="last_name" value="Lennon" id="id_last_name" />
 60<input type="text" name="birthday" value="1940-10-9" id="id_birthday" />""")
 61
 62        form_output = []
 63
 64        for boundfield in p:
 65            form_output.append([boundfield.label, boundfield.data])
 66
 67        self.assertEqual(form_output, [
 68            ['First name', u'John'],
 69            ['Last name', u'Lennon'],
 70            ['Birthday', u'1940-10-9']
 71        ])
 72        self.assertEqual(str(p), """<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>
 73<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="Lennon" id="id_last_name" /></td></tr>
 74<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>""")
 75
 76    def test_empty_dict(self):
 77        # Empty dictionaries are valid, too.
 78        p = Person({})
 79        self.assertTrue(p.is_bound)
 80        self.assertEqual(p.errors['first_name'], [u'This field is required.'])
 81        self.assertEqual(p.errors['last_name'], [u'This field is required.'])
 82        self.assertEqual(p.errors['birthday'], [u'This field is required.'])
 83        self.assertFalse(p.is_valid())
 84        try:
 85            p.cleaned_data
 86            self.fail('Attempts to access cleaned_data when validation fails should fail.')
 87        except AttributeError:
 88            pass
 89        self.assertEqual(str(p), """<tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr>
 90<tr><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr>
 91<tr><th><label for="id_birthday">Birthday:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="birthday" id="id_birthday" /></td></tr>""")
 92        self.assertEqual(p.as_table(), """<tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr>
 93<tr><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr>
 94<tr><th><label for="id_birthday">Birthday:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="birthday" id="id_birthday" /></td></tr>""")
 95        self.assertEqual(p.as_ul(), """<li><ul class="errorlist"><li>This field is required.</li></ul><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
 96<li><ul class="errorlist"><li>This field is required.</li></ul><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
 97<li><ul class="errorlist"><li>This field is required.</li></ul><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>""")
 98        self.assertEqual(p.as_p(), """<ul class="errorlist"><li>This field is required.</li></ul>
 99<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
100<ul class="errorlist"><li>This field is required.</li></ul>
101<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
102<ul class="errorlist"><li>This field is required.</li></ul>
103<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>""")
104
105    def test_unbound_form(self):
106        # If you don't pass any values to the Form's __init__(), or if you pass None,
107        # the Form will be considered unbound and won't do any validation. Form.errors
108        # will be an empty dictionary *but* Form.is_valid() will return False.
109        p = Person()
110        self.assertFalse(p.is_bound)
111        self.assertEqual(p.errors, {})
112        self.assertFalse(p.is_valid())
113        try:
114            p.cleaned_data
115            self.fail('Attempts to access cleaned_data when validation fails should fail.')
116        except AttributeError:
117            pass
118        self.assertEqual(str(p), """<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
119<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
120<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr>""")
121        self.assertEqual(p.as_table(), """<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
122<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
123<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr>""")
124        self.assertEqual(p.as_ul(), """<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
125<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
126<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>""")
127        self.assertEqual(p.as_p(), """<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
128<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
129<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>""")
130
131    def test_unicode_values(self):
132        # Unicode values are handled properly.
133        p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'})
134        self.assertEqual(p.as_table(), u'<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>\n<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></td></tr>\n<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>')
135        self.assertEqual(p.as_ul(), 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>')
136        self.assertEqual(p.as_p(), 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>')
137
138        p = Person({'last_name': u'Lennon'})
139        self.assertEqual(p.errors['first_name'], [u'This field is required.'])
140        self.assertEqual(p.errors['birthday'], [u'This field is required.'])
141        self.assertFalse(p.is_valid())
142        self.assertEqual(p.errors.as_ul(), u'<ul class="errorlist"><li>first_name<ul class="errorlist"><li>This field is required.</li></ul></li><li>birthday<ul class="errorlist"><li>This field is required.</li></ul></li></ul>')
143        self.assertEqual(p.errors.as_text(), """* first_name
144  * This field is required.
145* birthday
146  * This field is required.""")
147        try:
148            p.cleaned_data
149            self.fail('Attempts to access cleaned_data when validation fails should fail.')
150        except AttributeError:
151            pass
152        self.assertEqual(p['first_name'].errors, [u'This field is required.'])
153        self.assertEqual(p['first_name'].errors.as_ul(), u'<ul class="errorlist"><li>This field is required.</li></ul>')
154        self.assertEqual(p['first_name'].errors.as_text(), u'* This field is required.')
155
156        p = Person()
157        self.assertEqual(str(p['first_name']), '<input type="text" name="first_name" id="id_first_name" />')
158        self.assertEqual(str(p['last_name']), '<input type="text" name="last_name" id="id_last_name" />')
159        self.assertEqual(str(p['birthday']), '<input type="text" name="birthday" id="id_birthday" />')
160
161    def test_cleaned_data_only_fields(self):
162        # cleaned_data will always *only* contain a key for fields defined in the
163        # Form, even if you pass extra data when you define the Form. In this
164        # example, we pass a bunch of extra fields to the form constructor,
165        # but cleaned_data contains only the form's fields.
166        data = {'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9', 'extra1': 'hello', 'extra2': 'hello'}
167        p = Person(data)
168        self.assertTrue(p.is_valid())
169        self.assertEqual(p.cleaned_data['first_name'], u'John')
170        self.assertEqual(p.cleaned_data['last_name'], u'Lennon')
171        self.assertEqual(p.cleaned_data['birthday'], datetime.date(1940, 10, 9))
172
173    def test_optional_data(self):
174        # cleaned_data will include a key and value for *all* fields defined in the Form,
175        # even if the Form's data didn't include a value for fields that are not
176        # required. In this example, the data dictionary doesn't include a value for the
177        # "nick_name" field, but cleaned_data includes it. For CharFields, it's set to the
178        # empty string.
179        class OptionalPersonForm(Form):
180            first_name = CharField()
181            last_name = CharField()
182            nick_name = CharField(required=False)
183
184        data = {'first_name': u'John', 'last_name': u'Lennon'}
185        f = OptionalPersonForm(data)
186        self.assertTrue(f.is_valid())
187        self.assertEqual(f.cleaned_data['nick_name'], u'')
188        self.assertEqual(f.cleaned_data['first_name'], u'John')
189        self.assertEqual(f.cleaned_data['last_name'], u'Lennon')
190
191        # For DateFields, it's set to None.
192        class OptionalPersonForm(Form):
193            first_name = CharField()
194            last_name = CharField()
195            birth_date = DateField(required=False)
196
197        data = {'first_name': u'John', 'last_name': u'Lennon'}
198        f = OptionalPersonForm(data)
199        self.assertTrue(f.is_valid())
200        self.assertEqual(f.cleaned_data['birth_date'], None)
201        self.assertEqual(f.cleaned_data['first_name'], u'John')
202        self.assertEqual(f.cleaned_data['last_name'], u'Lennon')
203
204    def test_auto_id(self):
205        # "auto_id" tells the Form to add an "id" attribute to each form element.
206        # If it's a string that contains '%s', Django will use that as a format string
207        # into which the field's name will be inserted. It will also put a <label> around
208        # the human-readable labels for a field.
209        p = Person(auto_id='%s_id')
210        self.assertEqual(p.as_table(), """<tr><th><label for="first_name_id">First name:</label></th><td><input type="text" name="first_name" id="first_name_id" /></td></tr>
211<tr><th><label for="last_name_id">Last name:</label></th><td><input type="text" name="last_name" id="last_name_id" /></td></tr>
212<tr><th><label for="birthday_id">Birthday:</label></th><td><input type="text" name="birthday" id="birthday_id" /></td></tr>""")
213        self.assertEqual(p.as_ul(), """<li><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></li>
214<li><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></li>
215<li><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></li>""")
216        self.assertEqual(p.as_p(), """<p><label for="first_name_id">First name:</label> <input type="text" name="first_name" id="first_name_id" /></p>
217<p><label for="last_name_id">Last name:</label> <input type="text" name="last_name" id="last_name_id" /></p>
218<p><label for="birthday_id">Birthday:</label> <input type="text" name="birthday" id="birthday_id" /></p>""")
219
220    def test_auto_id_true(self):
221        # If auto_id is any True value whose str() does not contain '%s', the "id"
222        # attribute will be the name of the field.
223        p = Person(auto_id=True)
224        self.assertEqual(p.as_ul(), """<li><label for="first_name">First name:</label> <input type="text" name="first_name" id="first_name" /></li>
225<li><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li>
226<li><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li>""")
227
228    def test_auto_id_false(self):
229        # If auto_id is any False value, an "id" attribute won't be output unless it
230        # was manually entered.
231        p = Person(auto_id=False)
232        self.assertEqual(p.as_ul(), """<li>First name: <input type="text" name="first_name" /></li>
233<li>Last name: <input type="text" name="last_name" /></li>
234<li>Birthday: <input type="text" name="birthday" /></li>""")
235
236    def test_id_on_field(self):
237        # In this example, auto_id is False, but the "id" attribute for the "first_name"
238        # field is given. Also note that field gets a <label>, while the others don't.
239        p = PersonNew(auto_id=False)
240        self.assertEqual(p.as_ul(), """<li><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li>
241<li>Last name: <input type="text" name="last_name" /></li>
242<li>Birthday: <input type="text" name="birthday" /></li>""")
243
244    def test_auto_id_on_form_and_field(self):
245        # If the "id" attribute is specified in the Form and auto_id is True, the "id"
246        # attribute in the Form gets precedence.
247        p = PersonNew(auto_id=True)
248        self.assertEqual(p.as_ul(), """<li><label for="first_name_id">First name:</label> <input type="text" id="first_name_id" name="first_name" /></li>
249<li><label for="last_name">Last name:</label> <input type="text" name="last_name" id="last_name" /></li>
250<li><label for="birthday">Birthday:</label> <input type="text" name="birthday" id="birthday" /></li>""")
251
252    def test_various_boolean_values(self):
253        class SignupForm(Form):
254            email = EmailField()
255            get_spam = BooleanField()
256
257        f = SignupForm(auto_id=False)
258        self.assertEqual(str(f['email']), '<input type="text" name="email" />')
259        self.assertEqual(str(f['get_spam']), '<input type="checkbox" name="get_spam" />')
260
261        f = SignupForm({'email': 'test@example.com', 'get_spam': True}, auto_id=False)
262        self.assertEqual(str(f['email']), '<input type="text" name="email" value="test@example.com" />')
263        self.assertEqual(str(f['get_spam']), '<input checked="checked" type="checkbox" name="get_spam" />')
264
265        # 'True' or 'true' should be rendered without a value attribute
266        f = SignupForm({'email': 'test@example.com', 'get_spam': 'True'}, auto_id=False)
267        self.assertEqual(str(f['get_spam']), '<input checked="checked" type="checkbox" name="get_spam" />')
268
269        f = SignupForm({'email': 'test@example.com', 'get_spam': 'true'}, auto_id=False)
270        self.assertEqual(str(f['get_spam']), '<input checked="checked" type="checkbox" name="get_spam" />')
271
272        # A value of 'False' or 'false' should be rendered unchecked
273        f = SignupForm({'email': 'test@example.com', 'get_spam': 'False'}, auto_id=False)
274        self.assertEqual(str(f['get_spam']), '<input type="checkbox" name="get_spam" />')
275
276        f = SignupForm({'email': 'test@example.com', 'get_spam': 'false'}, auto_id=False)
277        self.assertEqual(str(f['get_spam']), '<input type="checkbox" name="get_spam" />')
278
279    def test_widget_output(self):
280        # Any Field can have a Widget class passed to its constructor:
281        class ContactForm(Form):
282            subject = CharField()
283            message = CharField(widget=Textarea)
284
285        f = ContactForm(auto_id=False)
286        self.assertEqual(str(f['subject']), '<input type="text" name="subject" />')
287        self.assertEqual(str(f['message']), '<textarea rows="10" cols="40" name="message"></textarea>')
288
289        # as_textarea(), as_text() and as_hidden() are shortcuts for changing the output
290        # widget type:
291        self.assertEqual(f['subject'].as_textarea(), u'<textarea rows="10" cols="40" name="subject"></textarea>')
292        self.assertEqual(f['message'].as_text(), u'<input type="text" name="message" />')
293        self.assertEqual(f['message'].as_hidden(), u'<input type="hidden" name="message" />')
294
295        # The 'widget' parameter to a Field can also be an instance:
296        class ContactForm(Form):
297            subject = CharField()
298            message = CharField(widget=Textarea(attrs={'rows': 80, 'cols': 20}))
299
300        f = ContactForm(auto_id=False)
301        self.assertEqual(str(f['message']), '<textarea rows="80" cols="20" name="message"></textarea>')
302
303        # Instance-level attrs are *not* carried over to as_textarea(), as_text() and
304        # as_hidden():
305        self.assertEqual(f['message'].as_text(), u'<input type="text" name="message" />')
306        f = ContactForm({'subject': 'Hello', 'message': 'I love you.'}, auto_id=False)
307        self.assertEqual(f['subject'].as_textarea(), u'<textarea rows="10" cols="40" name="subject">Hello</textarea>')
308        self.assertEqual(f['message'].as_text(), u'<input type="text" name="message" value="I love you." />')
309        self.assertEqual(f['message'].as_hidden(), u'<input type="hidden" name="message" value="I love you." />')
310
311    def test_forms_with_choices(self):
312        # For a form with a <select>, use ChoiceField:
313        class FrameworkForm(Form):
314            name = CharField()
315            language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')])
316
317        f = FrameworkForm(auto_id=False)
318        self.assertEqual(str(f['language']), """<select name="language">
319<option value="P">Python</option>
320<option value="J">Java</option>
321</select>""")
322        f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False)
323        self.assertEqual(str(f['language']), """<select name="language">
324<option value="P" selected="selected">Python</option>
325<option value="J">Java</option>
326</select>""")
327
328        # A subtlety: If one of the choices' value is the empty string and the form is
329        # unbound, then the <option> for the empty-string choice will get selected="selected".
330        class FrameworkForm(Form):
331            name = CharField()
332            language = ChoiceField(choices=[('', '------'), ('P', 'Python'), ('J', 'Java')])
333
334        f = FrameworkForm(auto_id=False)
335        self.assertEqual(str(f['language']), """<select name="language">
336<option value="" selected="selected">------</option>
337<option value="P">Python</option>
338<option value="J">Java</option>
339</select>""")
340
341        # You can specify widget attributes in the Widget constructor.
342        class FrameworkForm(Form):
343            name = CharField()
344            language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=Select(attrs={'class': 'foo'}))
345
346        f = FrameworkForm(auto_id=False)
347        self.assertEqual(str(f['language']), """<select class="foo" name="language">
348<option value="P">Python</option>
349<option value="J">Java</option>
350</select>""")
351        f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False)
352        self.assertEqual(str(f['language']), """<select class="foo" name="language">
353<option value="P" selected="selected">Python</option>
354<option value="J">Java</option>
355</select>""")
356
357        # When passing a custom widget instance to ChoiceField, note that setting
358        # 'choices' on the widget is meaningless. The widget will use the choices
359        # defined on the Field, not the ones defined on the Widget.
360        class FrameworkForm(Form):
361            name = CharField()
362            language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=Select(choices=[('R', 'Ruby'), ('P', 'Perl')], attrs={'class': 'foo'}))
363
364        f = FrameworkForm(auto_id=False)
365        self.assertEqual(str(f['language']), """<select class="foo" name="language">
366<option value="P">Python</option>
367<option value="J">Java</option>
368</select>""")
369        f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False)
370        self.assertEqual(str(f['language']), """<select class="foo" name="language">
371<option value="P" selected="selected">Python</option>
372<option value="J">Java</option>
373</select>""")
374
375        # You can set a ChoiceField's choices after the fact.
376        class FrameworkForm(Form):
377            name = CharField()
378            language = ChoiceField()
379
380        f = FrameworkForm(auto_id=False)
381        self.assertEqual(str(f['language']), """<select name="language">
382</select>""")
383        f.fields['language'].choices = [('P', 'Python'), ('J', 'Java')]
384        self.assertEqual(str(f['language']), """<select name="language">
385<option value="P">Python</option>
386<option value="J">Java</option>
387</select>""")
388
389    def test_forms_with_radio(self):
390        # Add widget=RadioSelect to use that widget with a ChoiceField.
391        class FrameworkForm(Form):
392            name = CharField()
393            language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=RadioSelect)
394
395        f = FrameworkForm(auto_id=False)
396        self.assertEqual(str(f['language']), """<ul>
397<li><label><input type="radio" name="language" value="P" /> Python</label></li>
398<li><label><input type="radio" name="language" value="J" /> Java</label></li>
399</ul>""")
400        self.assertEqual(f.as_table(), """<tr><th>Name:</th><td><input type="text" name="name" /></td></tr>
401<tr><th>Language:</th><td><ul>
402<li><label><input type="radio" name="language" value="P" /> Python</label></li>
403<li><label><input type="radio" name="language" value="J" /> Java</label></li>
404</ul></td></tr>""")
405        self.assertEqual(f.as_ul(), """<li>Name: <input type="text" name="name" /></li>
406<li>Language: <ul>
407<li><label><input type="radio" name="language" value="P" /> Python</label></li>
408<li><label><input type="radio" name="language" value="J" /> Java</label></li>
409</ul></li>""")
410
411        # Regarding auto_id and <label>, RadioSelect is a special case. Each radio button
412        # gets a distinct ID, formed by appending an underscore plus the button's
413        # zero-based index.
414        f = FrameworkForm(auto_id='id_%s')
415        self.assertEqual(str(f['language']), """<ul>
416<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
417<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
418</ul>""")
419
420        # When RadioSelect is used with auto_id, and the whole form is printed using
421        # either as_table() or as_ul(), the label for the RadioSelect will point to the
422        # ID of the *first* radio button.
423        self.assertEqual(f.as_table(), """<tr><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>
424<tr><th><label for="id_language_0">Language:</label></th><td><ul>
425<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
426<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
427</ul></td></tr>""")
428        self.assertEqual(f.as_ul(), """<li><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>
429<li><label for="id_language_0">Language:</label> <ul>
430<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
431<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
432</ul></li>""")
433        self.assertEqual(f.as_p(), """<p><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
434<p><label for="id_language_0">Language:</label> <ul>
435<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
436<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
437</ul></p>""")
438
439    def test_forms_wit_hmultiple_choice(self):
440        # MultipleChoiceField is a special case, as its data is required to be a list:
441        class SongForm(Form):
442            name = CharField()
443            composers = MultipleChoiceField()
444
445        f = SongForm(auto_id=False)
446        self.assertEqual(str(f['composers']), """<select multiple="multiple" name="composers">
447</select>""")
448
449        class SongForm(Form):
450            name = CharField()
451            composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')])
452
453        f = SongForm(auto_id=False)
454        self.assertEqual(str(f['composers']), """<select multiple="multiple" name="composers">
455<option value="J">John Lennon</option>
456<option value="P">Paul McCartney</option>
457</select>""")
458        f = SongForm({'name': 'Yesterday', 'composers': ['P']}, auto_id=False)
459        self.assertEqual(str(f['name']), '<input type="text" name="name" value="Yesterday" />')
460        self.assertEqual(str(f['composers']), """<select multiple="multiple" name="composers">
461<option value="J">John Lennon</option>
462<option value="P" selected="selected">Paul McCartney</option>
463</select>""")
464
465    def test_hidden_data(self):
466        class SongForm(Form):
467            name = CharField()
468            composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')])
469
470        # MultipleChoiceField rendered as_hidden() is a special case. Because it can
471        # have multiple values, its as_hidden() renders multiple <input type="hidden">
472        # tags.
473        f = SongForm({'name': 'Yesterday', 'composers': ['P']}, auto_id=False)
474        self.assertEqual(f['composers'].as_hidden(), '<input type="hidden" name="composers" value="P" />')
475        f = SongForm({'name': 'From Me To You', 'composers': ['P', 'J']}, auto_id=False)
476        self.assertEqual(f['composers'].as_hidden(), """<input type="hidden" name="composers" value="P" />
477<input type="hidden" name="composers" value="J" />""")
478
479        # DateTimeField rendered as_hidden() is special too
480        class MessageForm(Form):
481            when = SplitDateTimeField()
482
483        f = MessageForm({'when_0': '1992-01-01', 'when_1': '01:01'})
484        self.assertTrue(f.is_valid())
485        self.assertEqual(str(f['when']), '<input type="text" name="when_0" value="1992-01-01" id="id_when_0" /><input type="text" name="when_1" value="01:01" id="id_when_1" />')
486        self.assertEqual(f['when'].as_hidden(), '<input type="hidden" name="when_0" value="1992-01-01" id="id_when_0" /><input type="hidden" name="when_1" value="01:01" id="id_when_1" />')
487
488    def test_mulitple_choice_checkbox(self):
489        # MultipleChoiceField can also be used with the CheckboxSelectMultiple widget.
490        class SongForm(Form):
491            name = CharField()
492            composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=CheckboxSelectMultiple)
493
494        f = SongForm(auto_id=False)
495        self.assertEqual(str(f['composers']), """<ul>
496<li><label><input type="checkbox" name="composers" value="J" /> John Lennon</label></li>
497<li><label><input type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
498</ul>""")
499        f = SongForm({'composers': ['J']}, auto_id=False)
500        self.assertEqual(str(f['composers']), """<ul>
501<li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li>
502<li><label><input type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
503</ul>""")
504        f = SongForm({'composers': ['J', 'P']}, auto_id=False)
505        self.assertEqual(str(f['composers']), """<ul>
506<li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li>
507<li><label><input checked="checked" type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
508</ul>""")
509
510    def test_checkbox_auto_id(self):
511        # Regarding auto_id, CheckboxSelectMultiple is a special case. Each checkbox
512        # gets a distinct ID, formed by appending an underscore plus the checkbox's
513        # zero-based index.
514        class SongForm(Form):
515            name = CharField()
516            composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=CheckboxSelectMultiple)
517
518        f = SongForm(auto_id='%s_id')
519        self.assertEqual(str(f['composers']), """<ul>
520<li><label for="composers_id_0"><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li>
521<li><label for="composers_id_1"><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
522</ul>""")
523
524    def test_multiple_choice_list_data(self):
525        # Data for a MultipleChoiceField should be a list. QueryDict, MultiValueDict and
526        # MergeDict (when created as a merge of MultiValueDicts) conveniently work with
527        # this.
528        class SongForm(Form):
529            name = CharField()
530            composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=CheckboxSelectMultiple)
531
532        data = {'name': 'Yesterday', 'composers': ['J', 'P']}
533        f = SongForm(data)
534        self.assertEqual(f.errors, {})
535
536        data = QueryDict('name=Yesterday&composers=J&composers=P')
537        f = SongForm(data)
538        self.assertEqual(f.errors, {})
539
540        data = MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P']))
541        f = SongForm(data)
542        self.assertEqual(f.errors, {})
543
544        data = MergeDict(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])))
545        f = SongForm(data)
546        self.assertEqual(f.errors, {})
547
548    def test_multiple_hidden(self):
549        class SongForm(Form):
550            name = CharField()
551            composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=CheckboxSelectMultiple)
552
553        # The MultipleHiddenInput widget renders multiple values as hidden fields.
554        class SongFormHidden(Form):
555            name = CharField()
556            composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=MultipleHiddenInput)
557
558        f = SongFormHidden(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])), auto_id=False)
559        self.assertEqual(f.as_ul(), """<li>Name: <input type="text" name="name" value="Yesterday" /><input type="hidden" name="composers" value="J" />
560<input type="hidden" name="composers" value="P" /></li>""")
561
562        # When using CheckboxSelectMultiple, the framework expects a list of input and
563        # returns a list of input.
564        f = SongForm({'name': 'Yesterday'}, auto_id=False)
565        self.assertEqual(f.errors['composers'], [u'This field is required.'])
566        f = SongForm({'name': 'Yesterday', 'composers': ['J']}, auto_id=False)
567        self.assertEqual(f.errors, {})
568        self.assertEqual(f.cleaned_data['composers'], [u'J'])
569        self.assertEqual(f.cleaned_data['name'], u'Yesterday')
570        f = SongForm({'name': 'Yesterday', 'composers': ['J', 'P']}, auto_id=False)
571        self.assertEqual(f.errors, {})
572        self.assertEqual(f.cleaned_data['composers'], [u'J', u'P'])
573        self.assertEqual(f.cleaned_data['name'], u'Yesterday')
574
575    def test_escaping(self):
576        # Validation errors are HTML-escaped when output as HTML.
577        class EscapingForm(Form):
578            special_name = CharField(label="<em>Special</em> Field")
579            special_safe_name = CharField(label=mark_safe("<em>Special</em> Field"))
580
581            def clean_special_name(self):
582                raise ValidationError("Something's wrong with '%s'" % self.cleaned_data['special_name'])
583
584            def clean_special_safe_name(self):
585                raise ValidationError(mark_safe("'<b>%s</b>' is a safe string" % self.cleaned_data['special_safe_name']))
586
587        f = EscapingForm({'special_name': "Nothing to escape", 'special_safe_name': "Nothing to escape"}, auto_id=False)
588        self.assertEqual(f.as_table(), """<tr><th>&lt;em&gt;Special&lt;/em&gt; Field:</th><td><ul class="errorlist"><li>Something&#39;s wrong with &#39;Nothing to escape&#39;</li></ul><input type="text" name="special_name" value="Nothing to escape" /></td></tr>
589<tr><th><em>Special</em> Field:</th><td><ul class="errorlist"><li>'<b>Nothing to escape</b>' is a safe string</li></ul><input type="text" name="special_safe_name" value="Nothing to escape" /></td></tr>""")
590        f = EscapingForm({
591            'special_name': "Should escape < & > and <script>alert('xss')</script>",
592            'special_safe_name': "<i>Do not escape</i>"
593        }, auto_id=False)
594        self.assertEqual(f.as_table(), """<tr><th>&lt;em&gt;Special&lt;/em&gt; Field:</th><td><ul class="errorlist"><li>Something&#39;s wrong with &#39;Should escape &lt; &amp; &gt; and &lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;&#39;</li></ul><input type="text" name="special_name" value="Should escape &lt; &amp; &gt; and &lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;" /></td></tr>
595<tr><th><em>Special</em> Field:</th><td><ul class="errorlist"><li>'<b><i>Do not escape</i></b>' is a safe string</li></ul><input type="text" name="special_safe_name" value="&lt;i&gt;Do not escape&lt;/i&gt;" /></td></tr>""")
596
597    def test_validating_multiple_fields(self):
598        # There are a couple of ways to do multiple-field validation. If you want the
599        # validation message to be associated with a particular field, implement the
600        # clean_XXX() method on the Form, where XXX is the field name. As in
601        # Field.clean(), the clean_XXX() method should return the cleaned value. In the
602        # clean_XXX() method, you have access to self.cleaned_data, which is a dictionary
603        # of all the data that has been cleaned *so far*, in order by the fields,
604        # including the current field (e.g., the field XXX if you're in clean_XXX()).
605        class UserRegistration(Form):
606            username = CharField(max_length=10)
607            password1 = CharField(widget=PasswordInput)
608            password2 = CharField(widget=PasswordInput)
609
610            def clean_password2(self):
611                if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']:
612                    raise ValidationError(u'Please make sure your passwords match.')
613
614                return self.cleaned_data['password2']
615
616        f = UserRegistration(auto_id=False)
617        self.assertEqual(f.errors, {})
618        f = UserRegistration({}, auto_id=False)
619        self.assertEqual(f.errors['username'], [u'This field is required.'])
620        self.assertEqual(f.errors['password1'], [u'This field is required.'])
621        self.assertEqual(f.errors['password2'], [u'This field is required.'])
622        f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False)
623        self.assertEqual(f.errors['password2'], [u'Please make sure your passwords match.'])
624        f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False)
625        self.assertEqual(f.errors, {})
626        self.assertEqual(f.cleaned_data['username'], u'adrian')
627        self.assertEqual(f.cleaned_data['password1'], u'foo')
628        self.assertEqual(f.cleaned_data['password2'], u'foo')
629
630        # Another way of doing multiple-field validation is by implementing the
631        # Form's clean() method. If you do this, any ValidationError raised by that
632        # method will not be associated with a particular field; it will have a
633        # special-case association with the field named '__all__'.
634        # Note that in Form.clean(), you have access to self.cleaned_data, a dictionary of
635        # all the fields/values that have *not* raised a ValidationError. Also note
636        # Form.clean() is required to return a dictionary of all clean data.
637        class UserRegistration(Form):
638            username = CharField(max_length=10)
639            password1 = CharField(widget=PasswordInput)
640            password2 = CharField(widget=PasswordInput)
641
642            def clean(self):
643                if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']:
644                    raise ValidationError(u'Please make sure your passwords match.')
645
646                return self.cleaned_data
647
648        f = UserRegistration(auto_id=False)
649        self.assertEqual(f.errors, {})
650        f = UserRegistration({}, auto_id=False)
651        self.assertEqual(f.as_table(), """<tr><th>Username:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="username" maxlength="10" /></td></tr>
652<tr><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr>
653<tr><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr>""")
654        self.assertEqual(f.errors['username'], [u'This field is required.'])
655        self.assertEqual(f.errors['password1'], [u'This field is required.'])
656        self.assertEqual(f.errors['password2'], [u'This field is required.'])
657        f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False)
658        self.assertEqual(f.errors['__all__'], [u'Please make sure your passwords match.'])
659        self.assertEqual(f.as_table(), """<tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr>
660<tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr>
661<tr><th>Password1:</th><td><input type="password" name="password1" /></td></tr>
662<tr><th>Password2:</th><td><input type="password" name="password2" /></td></tr>""")
663        self.assertEqual(f.as_ul(), """<li><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></li>
664<li>Username: <input type="text" name="username" value="adrian" maxlength="10" /></li>
665<li>Password1: <input type="password" name="password1" /></li>
666<li>Password2: <input type="password" name="password2" /></li>""")
667        f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False)
668        self.assertEqual(f.errors, {})
669        self.assertEqual(f.cleaned_data['username'], u'adrian')
670        self.assertEqual(f.cleaned_data['password1'], u'foo')
671        self.assertEqual(f.cleaned_data['password2'], u'foo')
672
673    def test_dynamic_construction(self):
674        # It's possible to construct a Form dynamically by adding to the self.fields
675        # dictionary in __init__(). Don't forget to call Form.__init__() within the
676        # subclass' __init__().
677        class Person(Form):
678            first_name = CharField()
679            last_name = CharField()
680
681            def __init__(self, *args, **kwargs):
682                super(Person, self).__init__(*args, **kwargs)
683                self.fields['birthday'] = DateField()
684
685        p = Person(auto_id=False)
686        self.assertEqual(p.as_table(), """<tr><th>First name:</th><td><input type="text" name="first_name" /></td></tr>
687<tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>
688<tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>""")
689
690        # Instances of a dynamic Form do not persist fields from one Form instance to
691        # the next.
692        class MyForm(Form):
693            def __init__(self, data=None, auto_id=False, field_list=[]):
694                Form.__init__(self, data, auto_id=auto_id)
695
696                for field in field_list:
697                    self.fields[field[0]] = field[1]
698
699        field_list = [('field1', CharField()), ('field2', CharField())]
700        my_form = MyForm(field_list=field_list)
701        self.assertEqual(my_form.as_table(), """<tr><th>Field1:</th><td><input type="text" name="field1" /></td></tr>
702<tr><th>Field2:</th><td><input type="text" name="field2" /></td></tr>""")
703        field_list = [('field3', CharField()), ('field4', CharField())]
704        my_form = MyForm(field_list=field_list)
705        self.assertEqual(my_form.as_table(), """<tr><th>Field3:</th><td><input type="text" name="field3" /></td></tr>
706<tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr>""")
707
708        class MyForm(Form):
709            default_field_1 = CharField()
710            default_field_2 = CharField()
711
712            def __init__(self, data=None, auto_id=False, field_list=[]):
713                Form.__init__(self, data, auto_id=auto_id)
714
715                for field in field_list:
716                    self.fields[field[0]] = field[1]
717
718        field_list = [('field1', CharField()), ('field2', CharField())]
719        my_form = MyForm(field_list=field_list)
720        self.assertEqual(my_form.as_table(), """<tr><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr>
721<tr><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr>
722<tr><th>Field1:</th><td><input type="text" name="field1" /></td></tr>
723<tr><th>Field2:</th><td><input type="text" name="field2" /></td></tr>""")
724        field_list = [('field3', CharField()), ('field4', CharField())]
725        my_form = MyForm(field_list=field_list)
726        self.assertEqual(my_form.as_table(), """<tr><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr>
727<tr><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr>
728<tr><th>Field3:</th><td><input type="text" name="field3" /></td></tr>
729<tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr>""")
730
731        # Similarly, changes to field attributes do not persist from one Form instance
732        # to the next.
733        class Person(Form):
734            first_name = CharField(required=False)
735            last_name = CharField(required=False)
736
737            def __init__(self, names_required=False, *args, **kwargs):
738                super(Person, self).__init__(*args, **kwargs)
739
740                if names_required:
741                    self.fields['first_name'].required = True
742                    self.fields['first_name'].widget.attrs['class'] = 'required'
743                    self.fields['last_name'].required = True
744                    self.fields['last_name'].widget.attrs['class'] = 'required'
745
746        f = Person(names_required=False)
747        self.assertEqual(f['first_name'].field.required, f['last_name'].field.required, (False, False))
748        self.assertEqual(f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs, ({}, {}))
749        f = Person(names_required=True)
750        self.assertEqual(f['first_name'].field.required, f['last_name'].field.required, (True, True))
751        self.assertEqual(f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs, ({'class': 'required'}, {'class': 'required'}))
752        f = Person(names_required=False)
753        self.assertEqual(f['first_name'].field.required, f['last_name'].field.required, (False, False))
754        self.assertEqual(f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs, ({}, {}))
755
756        class Person(Form):
757            first_name = CharField(max_length=30)
758            last_name = CharField(max_length=30)
759
760            def __init__(self, name_max_length=None, *args, **kwargs):
761                super(Person, self).__init__(*args, **kwargs)
762
763                if name_max_length:
764                    self.fields['first_name'].max_length = name_max_length
765                    self.fields['last_name'].max_length = name_max_length
766
767        f = Person(name_max_length=None)
768        self.assertEqual(f['first_name'].field.max_length, f['last_name'].field.max_length, (30, 30))
769        f = Person(name_max_length=20)
770        self.assertEqual(f['first_name'].field.max_length, f['last_name'].field.max_length, (20, 20))
771        f = Person(name_max_length=None)
772        self.assertEqual(f['first_name'].field.max_length, f['last_name'].field.max_length, (30, 30))
773
774    def test_hidden_widget(self):
775        # HiddenInput widgets are displayed differently in the as_table(), as_ul())
776        # and as_p() output of a Form -- their verbose names are not displayed, and a
777        # separate row is not displayed. They're displayed in the last row of the
778        # form, directly after that row's form element.
779        class Person(Form):
780            first_name = CharField()
781            last_name = CharField()
782            hidden_text = CharField(widget=HiddenInput)
783            birthday = DateField()
784
785        p = Person(auto_id=False)
786        self.assertEqual(p.as_table(), """<tr><th>First name:</th><td><input type="text" name="first_name" /></td></tr>
787<tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>
788<tr><th>Birthday:</th><td><input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></td></tr>""")
789        self.assertEqual(p.as_ul(), """<li>First name: <input type="text" name="first_name" /></li>
790<li>Last name: <input type="text" name="last_name" /></li>
791<li>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></li>""")
792        self.assertEqual(p.as_p(), """<p>First name: <input type="text" name="first_name" /></p>
793<p>Last name: <input type="text" name="last_name" /></p>
794<p>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p>""")
795
796        # With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
797        p = Person(auto_id='id_%s')
798        self.assertEqual(p.as_table(), """<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
799<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
800<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></td></tr>""")
801        self.assertEqual(p.as_ul(), """<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
802<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
803<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>""")
804        self.assertEqual(p.as_p(), """<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
805<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
806<p><label for="id_b…

Large files files are truncated, but you can click here to view the full file