1 | import re
|
---|
2 |
|
---|
3 | from django.conf import settings
|
---|
4 | from django.forms import *
|
---|
5 |
|
---|
6 |
|
---|
7 | settings.configure(USE_I18N=False)
|
---|
8 |
|
---|
9 | class TestForm(Form):
|
---|
10 | field1 = CharField(label="field1", disabled=True)
|
---|
11 | field2 = CharField(label="field2", required=True)
|
---|
12 |
|
---|
13 | def 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 |
|
---|
21 | if __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'
|
---|