AJAX validation with Django, newforms and MochiKit

This weblog of mine presents the example code for using Django, newforms and MochiKit to do AJAX validation.
Before we get going, some context: the Christian Camps in Wales web site has a forum system which allows people to create polls. I rewrote this recently to use newforms, mainly to keep up to date with Django, and also to try out some AJAX.

I first had to write a ‘Form’ subclass which encapsulates the logic for validating and processing the form. As it happens, the form is based on a model ‘Poll’, but it requires more fields. In particular, this single form is used to create all the PollOption objects that are attached to the Poll. This is handled by a simple textarea field, into which all the options are entered on separate lines. In Django, we implement this using a custom ‘Field’ that uses the ‘Textarea’ widget and adds the relevant validation.Unlike classic web pages, which must load in their entirety if content changes, AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes

Sorry, comments are closed for this post.