Gregory Zhizhilkin
Inline forms in Django March 13, 2010 |
|
Task: | to make a convenient form using standard tools. |
||
For the convenience of users we often make complex forms which standard classes may not provide for.
For example, we want to make an HTML form composed of several fields and supplement it with a repeating set of fields.
Note |
In Django a form is a set of fields representing data of a single object. HTML form can be created using several Django forms. |
Repeating fields are created using the formsets module. Usually you need to describe the main form and the extended form and then display them in a template individually. But say we want to display repeating fields between simple form fields. The problem is that when using standard methods, a form in Django is displayed sequentially and continuously.
To place the repeating fields inside a simple form we can simply break down the form into individual fields in the template, but that conflicts with the DRY principle: we would have to duplicate all the code responsible for error messages, field names and text prompts.
Let’s create a field and a widget that would display the inline form.
|
|
We place a field of FieldsetField class where we need the form to contain an inline form:
|
|
Nothing changed in the template:
|
|
Multiplying fields will be covered in the next article.