AJAX validation with Django, newforms and MochiKit
Abstract: This tutorial 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.
|