1. Problem

How do we get the value of a variable from a submitted form?

2. Solution

There are several solutions.

The simplest is using request.get_form_var:

      def my_handler [html] (self, request):
          value = request.get_form_var('var_name_1')
          o
          o
          o

If you are using the quixote.form package, the .process() method places form values in a dictionary. This dictionary is then passed to the .action() method. Common usage looks like this:

class UserForm (Form):
    ...

    def process (self, request):
        values = Form.process(self, request)
        # Check that nickname isn't already used
        nick = values['nickname']
        if nick is not None:
            self.error['nickname'] = 'You must provide a nickname.'

        return values

    def action (self, request, submit, values):
        nickname = values['nickname']
        ...

If you are using the quixote.form2 package, you can reference the value of a widget in the form in the same way that you would reference a value associated with a key in a dictionary. Here is an example:

      myExampleForm = form2.Form(name='my_example_form', action_url='finish')
      myExampleForm.add(form2.StringWidget, 'user_name', '',
          title='User name:',
          hint='Enter your name (last, first). ',
          size=40,
          required=False)
      value = myExampleForm['user_name']
      o
      o
      o

3. Discussion

See UploadingFiles to see how to deal with uploaded files.


CategoryCookbook