Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Django – Handling multiple forms in single view
In Django web development, you often need to handle multiple forms within a single view. This article demonstrates how to create a function that manages two forms simultaneously using Django's MultiModelFormView.
Project Setup
Create a Django project and app. For this example, we'll use "multipleFormHandle" as the project name and "formhandlingapp" as the app name.
Complete the basic setup by including the app in settings.py INSTALLED_APPS and adding the app's URL to the project's main URL configuration.
Create forms.py in the app directory and a "templates" folder containing home.html.
Install the required library ?
pip install multi_form_view
Models
In models.py, create two models to store form data ?
from django.db import models
class StudentData(models.Model):
name = models.CharField(max_length=100)
standard = models.CharField(max_length=100)
section = models.CharField(max_length=100)
class TeacherData(models.Model):
name = models.CharField(max_length=100)
ClassTeacherOF = models.CharField(max_length=100)
Salary = models.CharField(max_length=100)
Forms
In forms.py, create ModelForms for both models ?
from django import forms
from .models import TeacherData, StudentData
class StudentForm(forms.ModelForm):
class Meta:
model = StudentData
fields = "__all__"
class TeacherForm(forms.ModelForm):
class Meta:
model = TeacherData
fields = "__all__"
Views
In views.py, create a class-based view using MultiModelFormView ?
from django.shortcuts import render
from .forms import StudentForm, TeacherForm
from django.urls import reverse
from multi_form_view import MultiModelFormView
class SchoolData(MultiModelFormView):
form_classes = {
'student_form': StudentForm,
'teacher_form': TeacherForm,
}
template_name = 'home.html'
def get_success_url(self):
return reverse('home')
def forms_valid(self, forms):
student = forms['student_form'].save(commit=False)
teacher = forms['teacher_form'].save(commit=False)
student.save()
teacher.save()
return super(SchoolData, self).forms_valid(forms)
URLs
In the app's urls.py ?
from django.urls import path
from . import views
urlpatterns = [
path('', views.SchoolData.as_view(), name='home'),
]
Template
In home.html, render both forms within a single form element ?
<!DOCTYPE html>
<html>
<head>
<title>Multiple Forms Example</title>
</head>
<body>
<form method="post">
{% csrf_token %}
<h3>Teacher Information</h3>
{{ forms.teacher_form.as_p }}
<h3>Student Information</h3>
{{ forms.student_form.as_p }}
<input type="submit" value="Submit Both Forms"/>
</form>
</body>
</html>
How It Works
The MultiModelFormView handles multiple forms by:
- form_classes: Defines which forms to include with custom names
- forms_valid: Processes all forms when they pass validation
- get_success_url: Defines redirect location after successful submission
Both forms share a single submit button and are validated together. If one form fails validation, neither form is saved.
Output
The application will display both Teacher and Student forms on the same page with a single submit button that processes both forms simultaneously.
Conclusion
Using MultiModelFormView simplifies handling multiple forms in Django by providing built-in validation and form processing. This approach is ideal when you need to collect related data through separate forms on a single page.
