Differences between revisions 2 and 3
Revision 2 as of 2009-11-20 04:23:59
Size: 2889
Editor: BenGolding
Comment:
Revision 3 as of 2009-11-20 04:26:53
Size: 2830
Editor: BenGolding
Comment:
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'

A more complex example

Here is a rather more complex example that uses a number of different input types.

Here's the model.

   1 class User:
   2    def __init__(self):
   3        self.name = ''
   4        self.an_int = 0
   5        self.a_float = 0.0
   6        self.country = 0
   7        self.password = ''
   8        self.active = False

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         buttons = Buttons((
  17                 Button('save', 'Save'),
  18                 Button('cancel', 'Cancel'),
  19             ))
  20         elements = (Fieldset(fields),)
  21         ctx.locals.test_form = FieldsetForm('User details',
  22                                             elements,
  23                                             buttons=buttons)

Render the form on the page:

That looks like this: attachment:forms-complex-1.png

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 =
   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.disabled = False
  18             return
  19         ctx.locals.test_form.disabled = True
  20         ctx.locals.test_form.merge(ctx.locals.description)
  21     elif ctx.req_equals('reset'):
  22         ctx.locals.test_form.clear()
  23         ctx.locals.test_form.disabled = False
  24     elif ctx.req_equals('cancel'):
  25         if ctx.locals.test_form.disabled:
  26             ctx.locals.test_form.disabled = False
  27         else:
  28             ctx.locals.test_form.clear()
  29             ctx.redirect('main')

None: A_more_complex_example (last edited 2011-02-15 06:05:17 by localhost)