1896
Comment:
|
← Revision 11 as of 2011-02-15 06:05:17 ⇥
3109
converted to 1.6 markup
|
Deletions are marked like this. | Additions are marked like this. |
Line 21: | Line 21: |
'Australia', 'Belgium', 'Cuba', 'Greenland', 'Madagascar', 'Netherlands', 'Switzerland', 'Uzbekistan', 'Zimbabwe' |
'Australia', 'Belgium', 'Cuba', 'Greenland', 'Madagascar', 'Netherlands', 'Switzerland', 'Uzbekistan', 'Zimbabwe' |
Line 39: | Line 33: |
Checkbox('Active', 'active'),) | Checkbox('Active', 'active'), ) |
Line 41: | Line 36: |
Button('save', 'Save'), Button('cancel', 'Cancel'), )) elements = (Fieldset(fields),) |
Button('Save', 'save'), Button('Cancel', 'cancel'), )) fieldsets = (Fieldset(fields), ) |
Line 46: | Line 41: |
elements, | fieldsets, |
Line 60: | Line 55: |
attachment:forms-complex-1.png | {{attachment:forms-complex-1.png}} |
Line 68: | Line 63: |
In the {{{forms.py}}} file, the code looks like: {{{#!python def page_enter(ctx): if not ctx.has_value('test_form'): ctx.locals.description = User() ctx.add_session_vars('description') ctx.locals.test_form = [see above] def page_display(ctx): ctx.run_template('forms.html') def page_process(ctx): if ctx.req_equals('save'): try: ctx.locals.test_form.validate() except FormValidationError, e: ctx.locals.test_form.set_disabled(False) return ctx.locals.test_form.set_disabled(True) # This disables the FieldSet! # update the values in the model from the form ctx.locals.test_form.merge(ctx.locals.description) # now you can do things with the updated object, eg ctx.log('user %s, active %s' % (ctx.locals.description.user, ctx.locals.description.active)) elif ctx.req_equals('reset'): ctx.locals.test_form.clear() ctx.locals.test_form.set_disabled(False) elif ctx.req_equals('cancel'): if ctx.locals.test_form.disabled: ctx.locals.test_form.set_disabled(False) else: ctx.locals.test_form.clear() ctx.redirect('main') }}} |
A more complex example
Here is a rather more complex example that uses a number of different input types.
Here's the model.
The form that describes how we want it laid out
1 # Slightly abridged list of all the countries in the world.
2 country_names = (
3 'Australia', 'Belgium', 'Cuba',
4 'Greenland', 'Madagascar', 'Netherlands',
5 'Switzerland', 'Uzbekistan', 'Zimbabwe'
6 )
7 country_menu = [e for e in enumerate(country_names)]
8 fields = (
9 TextField('Name', 'name', required=True),
10 IntegerField('Integer', 'an_int'),
11 FloatField('Float', 'a_float'),
12 SelectField('Country', country_menu, 'country'),
13 PasswordField('Password', 'password',
14 required=True),
15 Checkbox('Active', 'active'),
16 )
17 buttons = Buttons((
18 Button('Save', 'save'),
19 Button('Cancel', 'cancel'),
20 ))
21 fieldsets = (Fieldset(fields), )
22 ctx.locals.test_form = FieldsetForm('User details',
23 fieldsets,
24 buttons=buttons)
Render the form on the page:
To render the form as static report:
In the forms.py file, the code looks like:
1 def page_enter(ctx):
2 if not ctx.has_value('test_form'):
3 ctx.locals.description = User()
4 ctx.add_session_vars('description')
5 ctx.locals.test_form = [see above]
6
7
8 def page_display(ctx):
9 ctx.run_template('forms.html')
10
11
12 def page_process(ctx):
13 if ctx.req_equals('save'):
14 try:
15 ctx.locals.test_form.validate()
16 except FormValidationError, e:
17 ctx.locals.test_form.set_disabled(False)
18 return
19 ctx.locals.test_form.set_disabled(True) # This disables the FieldSet!
20 # update the values in the model from the form
21 ctx.locals.test_form.merge(ctx.locals.description)
22 # now you can do things with the updated object, eg
23 ctx.log('user %s, active %s' % (ctx.locals.description.user, ctx.locals.description.active))
24 elif ctx.req_equals('reset'):
25 ctx.locals.test_form.clear()
26 ctx.locals.test_form.set_disabled(False)
27 elif ctx.req_equals('cancel'):
28 if ctx.locals.test_form.disabled:
29 ctx.locals.test_form.set_disabled(False)
30 else:
31 ctx.locals.test_form.clear()
32 ctx.redirect('main')