[Web-SIG] Standalone WSGI form framework.

Daniel Miller millerdev at gmail.com
Thu Mar 16 06:21:19 CET 2006


Alan Kennedy wrote:
> I'm looking for a framework-independent form library. I'm using the
> Quixote forms library at the moment, inside my own framework, but
> would ideally like something more WSGI oriented, so that it is easier
> to mock and unittest.

Have you looked at Ian Bicking's FormEncode? I'm not sure if it meets all your requirements, but it seems like a good base to start with (most of the hard stuff has already been done).

> 1. Parsing of submitted POST requests, etc

FormEncode (along with VariableDecode, a subcomponent) can do this. VariableDecode takes a flat dict and turns it into hierarchical structure of lists and dictionaries. FormEncode applies a schema or group of validators to this hierarchical structure, converting strings to python objects. Here FormEncode is a bit different than Spring: all validation occurs completely separate from binding.

> 2. Binding of incoming form variables to the attributes of a target
> python data object

Once they've been validated by FormEncode, it fairly trivial to write code to bind values to objects. Binding is much simpler in Python than it is in Java because Python has a simpler reflection api (and Python is dynamic).

> 3. Customisable validation, with management of validation error messages.

FormEncode has the most flexible custom validation I've ever used. I wished Spring had something like it before I knew about FormEncode.

> 4. Generate unique (hierarchical) field names for sub-attributes of
> the data object to be edited, which are javascript-identifier-safe,
> i.e. can be used as the names of HTML form input elements.

I'm pretty sure FormEncode does this, and if it doesn't it's very simple to write your own "validator" that will do it.

> 5. Handle multipart/form-data

FormEncode has a FieldStorageUploadConverter (I think it's for this type of thing).

> 6. Nice-to-have: transparently handle multi-page forms, e.g. hub forms, etc.

FormEncode can do partial validation of a given schema, but I'd probably just write a separate schema for each page and then use them all together on the last page (easily done).

> It should NOT
> 
> 1. Attempt to generate HTML, or be tied to a specific templating mechanism

As long as you stay away from HTMLForm (is it deprecated??) I think FormEncode is fine here too.

> 
> If anyone is familiar with the Java Spring Framework, it's got pretty
> much everything I need, but is overly complex, and is written in Java
> :-(
> 

I wrote an app using Spring and I have to say it's the best web framework I've ever used in terms of completeness and flexibility, but it's written in Java... I actually wrote a few simple classes on top of CherryPy that exposes the Spring webmvc Controller interface as well as the SimpleFormController class (those are the two main building blocks I found most useful in Spring's WebMVC). My SimpleFormController implementation uses FormEncode for validation. I'd be willing to share the code if you're interested.

I think "the one true web framework" could be made for Python if someone took the best ideas from Spring WebMVC and made a few component-ized building blocks on top of which complex and widely varied applications could be built. However, to make this possible we'd most likely need a standard request object (or at least an interface definition).

~ Daniel



More information about the Web-SIG mailing list