Form.fields
You can access the fields of Form
instance from its fields
attribute:
1 2 3 4 5 6 7 | >>> for row in f.fields.values(): print (row) ... <django.forms.fields.CharField object at 0x7ffaac632510 > <django.forms.fields.URLField object at 0x7ffaac632f90 > <django.forms.fields.CharField object at 0x7ffaac3aa050 > >>> f.fields[ 'name' ] <django.forms.fields.CharField object at 0x7ffaac6324d0 > |
You can alter the field of Form
instance to change the way it is presented in the form:
1 2 3 4 5 | >>> f.as_table().split( '\n' )[ 0 ] '<tr><th>Name:</th><td><input name="name" type="text" value="instance" required /></td></tr>' >>> f.fields[ 'name' ].label = "Username" >>> f.as_table().split( '\n' )[ 0 ] '<tr><th>Username:</th><td><input name="name" type="text" value="instance" required /></td></tr>' |
Beware not to alter the base_fields
attribute because this modification will influence all subsequent ContactForm
instances within the same Python process:
1 2 3 4 | >>> f.base_fields[ 'name' ].label = "Username" >>> another_f = CommentForm(auto_id = False ) >>> another_f.as_table().split( '\n' )[ 0 ] '<tr><th>Username:</th><td><input name="name" type="text" value="class" required /></td></tr>' |
Please login to continue.