Add multiple forms in a page in Django

We often need to add more than one form in a webpage of our Django website. This requirement arises in a lot of situations. There might be a comment form and a feedback form on our page. There could be many more situations where we need to add multiple forms to our page in Django.

In this tutorial, we’ll learn how to add multiple forms on a single page to our Django website. Trust me, this is going to be very useful!

First Things First!

Let’s create a django project and a django app:

django-admin startproject multiple_forms
cd multiple_forms
django-admin startapp app

My project is ‘mutliple_forms’ and I’ve created an app with name ‘app’ (Seriously! Suggest me some good name for my Django apps).

Configuring our root urls (multiple_forms/urls.py):

Add the lines below to your multiple_forms/urls.py:

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app.urls'))
]

Let’s add our app name (which is ‘app‘ itself) to the list of INSTALLED_APPS in our multiple_forms/settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app' # Add here!
]

I’m not configuring my template lookup path. I’ll create a templates folder in my app itself.

Making our app

Create a urls.py file in our app folder and add the following lines to it:

from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name="index")
]

Great! Now let’s create a templates folder inside our app and create an index.html file inside this templates folder. Add the below content to it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multiple Forms</title>
</head>
<body>
    <form action="" method="post">
        {% csrf_token %}
        <label for="">Name 1:</label><br>
        <input type="text" name="name1">
        <br>
        <input type="submit" name="form1">
    </form>
    <form action="" method="post">
        {% csrf_token %}
        <label for="">Name 2:</label><br>
        <input type="text" name="name2">
        <br>
        <input type="submit" name="form2">
    </form>
</body>
</html>

As you can observe, I’m creating two forms here. These forms will get uniquely identified by the names (I’ve given ‘form1’ and ‘form2’ here) given to the submit buttons for the respective forms.

Now we’re left with one last thing, Our views function! Add the following lines to your app/views.py:

from django.shortcuts import render
def index(request):
    if request.method == "GET":
        return render(request, 'index.html')
    elif request.method == "POST":
        if 'form1' in request.POST:
            name1 = request.POST['name1']
            print('Name1: ',name1)
        if 'form2' in request.POST:
            name2 = request.POST['name2']
            print('Name2: ',name2)
        return render(request, 'index.html')

For simplicity I’m just printing out the data from the forms to the console. But you can do any complex operation with this data (Please do). And finally I’m rendering a new empty form.

Yeah, we’re done. Run the server using:

python manage.py runserver

You should be able to submit data through both the forms (Not simultaneously, you can only click submit button of one form at a time right?).

Check out the source code here: https://github.com/Chandu-4444/Multiple-Forms