Solving problems with Django Forms.

Kirt Gittens

Monday 2:50 p.m.–3:20 p.m.
Audience level: Intermediate or Advanced

Description

Out of the box, Django forms offers a lot of functionality, but in order to tackle advanced problems, You may need to deep dive into the forms API, as well as look at some external libraries that can provide assistance in rendering and validating your form. This talk takes a look at some complex problems that were solved with Django forms, and what can be learned from their solutions.

Abstract

We'll look at a few core problems that we were able to solve with Django forms.

  1. Dynamic Field Creation: What if you don't know what fields should be present on a Django form until runtime?. Solutions:

  2. Viewing a form's fields as a data structure (convert a field definition to a dictionary) Manipulate self.fields on a form to dynamically add / remove forms from a field.

Pitfalls:

A fields validated attributes can't be manipulated dynamically because of Validators within the forms API. Dynamic form layouts become difficult to manage, crispyforms does not scale as a solution!

  1. Validate a form via an API: How can external validations behave the same as internal errors? Solutions:

  2. form.clean() can be used for form wide errors, and form.add_error can be used to integrate those external validation errors into your existing form so that calls like is_valid() still work as expected with your external validations.

  3. Adding fields at runtime: How can the user add fields to a form after it has been rendered?

Solutions:

Javascript can be used for the UI, and if the fields are properly named, the same validations will work as long as the fields are part of the form.

Pitfalls: Creating a solution that creates a dynamic field that is validated, but doesn't render can cause issues with your layout solution (crispyforms fails again here)


Buy your conference and tutorial tickets here! Tutorials are $150 per session.