Ticket #26129: disabled.py

File disabled.py, 1.2 KB (added by Gerald Thibault, 9 years ago)

An example of a disabled field displaying the value in an unbound form, but omitting the value in a bound form.

Line 
1import re
2
3from django.conf import settings
4from django.forms import *
5
6
7settings.configure(USE_I18N=False)
8
9class TestForm(Form):
10 field1 = CharField(label="field1", disabled=True)
11 field2 = CharField(label="field2", required=True)
12
13def get_value(form, field):
14 " checks if field1 initial value is present "
15 p = re.compile('<input id="id_%s"[^>]*value="([^"]*)"[^>]*>' % field)
16 match = p.search(str(form))
17 if not match:
18 return None
19 return match.group(1)
20
21if __name__ == '__main__':
22 initial_field = 'field1'
23 initial_value = 'static'
24 initial = {initial_field: initial_value}
25
26 # unbound form
27 print 'testing unbound form'
28 form = TestForm(initial=initial)
29 value = get_value(form, initial_field)
30 if value == initial_value:
31 print ' initial value found in formfield'
32 else:
33 print ' initial value missing from formfield'
34
35 # bound form
36 print 'testing bound form'
37 data = {}
38 form = TestForm(data, initial=initial)
39 value = get_value(form, initial_field)
40 if value == initial_value:
41 print ' initial value found in formfield'
42 else:
43 print ' initial value missing from formfield'
Back to Top