Although we iterated over the book search form example several times and improved it nicely, it's still fundamentally simple: just a single field, 'q'
. As forms get more complex, we have to repeat the preceding steps over and over again for each form field we use. This introduces a lot of cruft and a lot of opportunities for human error. Lucky for us, the Django developers thought of this and built into Django a higher-level library that handles form-and validation-related tasks.
Django comes with a form library, called django.forms
, that handles many of the issues we've been exploring this chapter-from HTML form display to validation. Let's dive in and rework our contact form application using the Django forms framework.
The primary way to use the forms framework is to define a Form
class for each HTML <form>
you're dealing with. In our case, we only have one <form>
, so we'll have one Form
class. This class can live anywhere you want...