Ticket #3334: form_tests.diff

File form_tests.diff, 1.8 KB (added by Deryck Hodge <deryck@…>, 18 years ago)

A patch to tests/regressiontests/forms/tests.py to test instances of dynamically created forms return the right fields.

  • tests/regressiontests/forms/tests.py

     
    20632063>>> f.clean_data
    20642064{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}
    20652065
     2066# Dynamic construction ########################################################
     2067
    20662068It's possible to construct a Form dynamically by adding to the self.fields
    20672069dictionary in __init__(). Don't forget to call Form.__init__() within the
    20682070subclass' __init__().
     
    20782080<tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>
    20792081<tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>
    20802082
     2083Instances of a dynamic form should not persist fields from one
     2084form instance to the next
     2085>>> class MyForm(Form):
     2086...     def __init__(self, data=None, auto_id=False, field_list=[]):
     2087...         Form.__init__(self, data, auto_id)
     2088...         for field in field_list:
     2089...             self.fields[field[0]] = field[1]
     2090...
     2091>>> field_list = ( ('field1', CharField()), ('field2', CharField()) )
     2092>>> my_form = MyForm(field_list=field_list)
     2093>>> print my_form
     2094<tr><th>Field1:</th><td><input type="text" name="field1" /></td></tr>
     2095<tr><th>Field2:</th><td><input type="text" name="field2" /></td></tr>
     2096>>> field_list = ( ('field3', CharField()), ('field4', CharField()) )
     2097>>> my_form = MyForm(field_list=field_list)
     2098>>> print my_form
     2099<tr><th>Field3:</th><td><input type="text" name="field3" /></td></tr>
     2100<tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr>
     2101
    20812102HiddenInput widgets are displayed differently in the as_table(), as_ul()
    20822103and as_p() output of a Form -- their verbose names are not displayed, and a
    20832104separate row is not displayed. They're displayed in the last row of the
Back to Top