Cover by Dan Sanderson

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Using Django Forms

Django includes a powerful feature for building web forms based on data model definitions. The Django forms library can generate HTML for forms, validate that submitted data meets the requirements of the model, and redisplay the form to the user with error messages. The default appearance is useful, and you can customize the appearance extensively.

Django’s version of the form library only works with the Django data modeling API, and does not work directly with App Engine models. Thankfully, the App Engine Python runtime environment includes an App Engine–compatible implementation of Django forms, in the package google.appengine.ext.db.djangoforms.

We won’t go into the details of how Django forms work—see the Django documentation for a complete explanation—but let’s walk through a quick example to see how the pieces fit together. Our example will use the following behavior for creating and editing Book entities:

  • An HTTP GET request to /books/book/ displays an empty form for creating a new Book.

  • An HTTP POST request to /books/book/ processes the book creation form, and either creates the book and redirects to /books (the book listing page) or redisplays the form with errors, if any.

  • An HTTP GET request to /books/book/1234 displays the form to edit the Book entity, with the fields filled out with the current values.

  • An HTTP POST request to /books/book/1234 updates the book with that ID, with the same error-handling behavior as the book creation form.

Edit urls.py to use ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required