Formsets
In Django, forms are a great way to handle HTTP requests data. But what if you need to deal with a set of similar forms in a single view? That's where Django's Formsets come into play.
What Are Formsets?
Formsets in Django are essentially a layer of abstraction to work with multiple forms on the same page. These are very handy when you need to work with multiple similar forms, for example, when handling multiple user inputs at once or managing a group of related forms.
Creating a Basic Formset
To create a formset, Django provides a function called formset_factory
. Here is an example of how to create a basic formset:
from django.forms import formset_factory
from .forms import YourForm
YourFormSet = formset_factory(YourForm)
In the above example, YourForm
is the form that you want to repeat in the formset. YourFormSet
is now a class that you can use to create instances of your formset.
Using a Formset in a View
Once you have a formset, you can use it in a view like this:
from django.shortcuts import render
from .forms import YourFormSet
def your_view(request):
if request.method == 'POST':
formset = YourFormSet(request.POST)
if formset.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
else:
formset = YourFormSet()
return render(request, 'your_template.html', {'formset': formset})
In the above view, if the request is a POST (which means the form is being submitted), the formset is initialized with request.POST
data. If the formset is valid, you can process the data and then redirect the user to a new URL.
If the request is not a POST (which means the form is being displayed for the first time), an empty formset is created.
The formset is then passed to the template in the context dictionary.
Displaying a Formset in a Template
In your template, you can display the formset like this:
<form method="POST">
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
<input type="submit" value="Submit">
</form>
Here, formset.management_form
is a hidden form that's included with each formset. It contains management data for the set of forms that are included in the formset.
The {% for form in formset %}
loop is used to display each individual form in the formset.
Adding Extra Forms
By default, a formset displays one empty form. If you want to display more than one empty form, you can do so by passing the extra
parameter to the formset_factory
function:
YourFormSet = formset_factory(YourForm, extra=3)
This will display three empty forms.
In Conclusion
Formsets are a powerful feature of Django that allow you to work with multiple similar forms on the same page. They provide a layer of abstraction to manage a collection of form instances.
Remember that formsets are not a replacement for forms, but a way to manage multiple instances of a form. You should still create and validate your forms as you would normally do. Formsets just make it easier to work with multiple forms at once.
Happy coding!