[Answer]-Using forms only for validation


If the form and model match nicely then I’ll take advantage of the ModelForm functionality.
But most of the time it is not so tidy so, most typically, I do things in about this order:

  1. create a django form with all the field definitions
  2. create django GET view to serve the empty form
  3. create an html template which serves the default html/form
  4. test the blank form
  5. create the POST routine to call validation and reserve the validated (erroneous) form
  6. modify the django form to validate the fields
  7. modify the html form to serve the error messages
  8. test the validation and error messages
  9. modify the POST routine to handle a valid form and do whatever it should do as a result (might involve a redirect and ‘thanks’ view/template)
  10. Test the whole lot
  11. let the designer loose on the templates

In truth the designer will be involved at some points earlier along the way but in theory I just get it all to work as a “white” then add all the fancy stuff after. That includes javascript validation (ie after all the above).


I ended up doing something like this. It is ugly, and may not be the proper way to do it, but it works…

if request.method == 'POST':
         # Create dictionary from POST data
         data = {
                    'foo': request.POST['foo'],
                    'foobar': request.POST['foobar'],
         # Handle exceptions

     form = ImportedForm(data)
     if form.is_valid: 
         # Continue to validate and save

Leave a comment