Open In App

Django Formsets

Last Updated : 24 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Example: Creating a Simple Form and Formset

Step 1: Define a Basic Form

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)

Step 2: Create a Formset in Views

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})

Step 3: Render the Formset in a Template

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-formsets
Django Formset

Our formset is working completely. Let's learn how to modify this formset to use extra features of this formset.

Step 4: Handling Multiple Forms with extra

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:

django-formsets-multiple-forms
localhost

Step 5: Processing Submitted Formset Data

To 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:

enter-formset-data-django-formsets

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

django-formsets-use-data1

Read Next Article: Django ModelFormSets


Next Article
Practice Tags :

Similar Reads