[Web-SIG] Re: Form field dictionaries

Simon Willison cs1spw at bath.ac.uk
Fri Oct 24 12:31:13 EDT 2003

Ian Bicking wrote:
> I think this is already really decided -- if (and only if) there are 
> multiple values, then a list should appear in the output.  I.e., {'a': 
> ['1', '2']}.  This is how cgi works, and how almost all Python request 
> objects work.

I don't have enough practical Python web development experience to back 
this up, but it seems to me that this could lead to an awful lot of 
unhandled exceptions. For example:

username = GET['username'].lower()

This would work fine provided no one fed two username values to the 
script, at which point it would die with an exception:

Traceback (most recent call last):
   File "<pyshell#44>", line 1, in -toplevel-
AttributeError: 'list' object has no attribute 'lower'

Adding exception handling to every piece of code that accesses string 
values from a form field dictionary would be a pretty tall order.

One alternative might be some kind of enhanced form field access object 
that adds a layer of validation. For example:

form = web.cgi.ValidatingForm()

     username = form.get_string('username')
     id = form.get_int('id')
     permissions = form.get_list('permissions')
except ValidationError:
     print 'Invalid form data'

Form validation like this though is really a whole other topic.

Simon Willison
Web development weblog: http://simon.incutio.com/

More information about the Web-SIG mailing list