Snippet List
If using javascript is not an option, you can use something like this code to have a variable number of subforms.
This code uses crispy-forms, but it is totally dispensable.
- formset
- dynamic-formset
- formset_factory
I recently worked on an application, where I had to provide a way for users to search for objects based on user-defined properties attached to these objects. I decided to model the search form using a formset, and I thought it'd be a good idea to allow users dynamically add and remove search criteria.
The script (dynamic-formset.js) should be re-usable as-is:
1. Include it in your template (don't forget to include jquery.js first!).
2. Apply the 'dynamic-form' class to the container for each form instance (in this example, the 'tr').
3. Handle the 'click' event for your `add` and `delete` buttons. Call the `addForm` and `deleteForm` functions respectively, passing each function a reference to the button raising the event, and the formset prefix.
That's about it. In your view, you can instantiate the formset, and access your forms as usual.
- newforms
- jquery
- dynamic-formset
2 snippets posted so far.