Django Formsets allow you to manage multiple instances of the same form on a single webpage easily. Instead of creating separate forms one by one, formsets let you group them together so you can display, validate and process all of them at once. Think of a formset like a spreadsheet where each row is a form.
For example, if you want users to add multiple articles at once, a formset lets you show several article forms together, just like rows in a table, making it easier to manage many inputs in one place.
In this article we will learn to work with formsets in Django, so first make sure to create and setup a django project and app:
In your Django app (e.g., geeks), create or open forms.py and define a simple form:
Python
from django import forms
class GeeksForm(forms.Form):
title = forms.CharField()
description = forms.CharField()
Let's explain what exactly is happening, left side denotes the name of the field and to right of it, you define various functionalities of an input field correspondingly. A field's syntax is:
Syntax:
Field_name = forms.FieldType(attributes)
In views.py, import formset_factory and your form, then create a view to render the formset:
Python
from django.shortcuts import render
from django.forms import formset_factory
from .forms import GeeksForm
def formset_view(request):
GeeksFormSet = formset_factory(GeeksForm)
formset = GeeksFormSet()
return render(request, "home.html", {'formset': formset})
Create or edit your template file (templates/home.html) to display the formset:
html
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.as_p }}
<input type="submit" value="Submit">
</form>
Note: The management_form is required for Django to keep track of formset data and handle validation properly.
All set to check if our formset is working or not, run the development server using command:
python manage.py runserver
And then visit http://localhost:8000/
Django FormsetOur formset is working completely. Let's learn how to modify this formset to use extra features of this formset.
By default, a formset displays only one form. You can specify how many empty forms to show using the extra parameter:
GeeksFormSet = formset_factory(GeeksForm, extra=5) # Shows 5 forms
formset = GeeksFormSet()
The keyword argument "extra"
makes multiple copies of same form. If one wants to create 5 forms enter extra = 5
and similarly for others.
Visit: http://localhost:8000/ to check if 5 forms are created:
localhostTo handle submitted data, update your view to bind POST data and validate:
Python
def formset_view(request):
GeeksFormSet = formset_factory(GeeksForm, extra=3)
if request.method == 'POST':
formset = GeeksFormSet(request.POST)
if formset.is_valid():
for form in formset:
print(form.cleaned_data) # Process form data here
else:
formset = GeeksFormSet()
return render(request, "home.html", {'formset': formset})
Now let's try to enter data in the formset through:

Hit submit and data will be display in command line where server is running:

Read Next Article: Django ModelFormSets
Similar Reads
Django Form Django Forms are used to gather input from users, validate that input, and process it, often saving the data to the database. For example, when registering a user, a form collects information like name, email, and password.Django automatically maps form fields to corresponding HTML input elements. I
5 min read
How to create a form using Django Forms ? This article explains how to create a basic form using various form fields and attributes. Creating a form in Django is very similar to creating a model, you define the fields you want and specify their types. For example, a registration form might need fields like First Name (CharField), Roll Numbe
2 min read
Django ModelFormSets Django ModelFormsets provide a powerful way to manage multiple model-based forms on a single page. They allow you to create, update, or delete multiple instances of a model at once, using a group of forms that correspond to the model's fields.Think of ModelFormsets as a collection of forms linked to
2 min read
Django ModelForm ModelForm is a class that automatically generates a "form" from a Django model. It reduces boilerplate code by linking your form fields directly to your model fields, making form creation faster, cleaner, and less error-prone. It also provides built-in methods and validation to streamline form proce
3 min read
Render Django Forms as table Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as paragraph Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render Django Forms as list Django forms are an advanced set of HTML forms that can be created using python and support all features of HTML forms in a pythonic way. Rendering Django Forms in the template may seem messy at times but with proper knowledge of Django Forms and attributes of fields, one can easily create excellent
2 min read
Render HTML Forms (GET & POST) Django is often called a "Batteries Included Framework" because it provides built-in settings and features that help developers build websites rapidly and efficiently. One of the essential components in web development is handling HTML forms, a way for users to send data to the server for processing
3 min read
Django form field custom widgets A widget is Djangoâs representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
3 min read
Initial form data _ Django Forms When using Django forms, we may want to automatically fill in some fields with default values. This is called initial data and it's different from placeholders because it actually fills the fields. When the form is submitted, this data is treated just like anything the user types in.Django offers mu
3 min read
Render Django Form Fields Manually Django form fields have several built-in methods to ease the work of the developer but sometimes one needs to implement things manually for customizing User Interface(UI). We have already covered on How to create and use a form in Django?. A form comes with 3 in-built methods that can be used to ren
5 min read
Django Formsets Django Formsets allow you to manage multiple instances of the same form on a single webpage easily. Instead of creating separate forms one by one, formsets let you group them together so you can display, validate and process all of them at once. Think of a formset like a spreadsheet where each row i
3 min read
Django Forms Django Forms are used to gather input from users, validate that input, and process it, often saving the data to the database. For example, when registering a user, a form collects information like name, email, and password.Django automatically maps form fields to corresponding HTML input elements. I
5 min read