We sometimes need to handle multiple forms in a single function or view. In this article, we will see how to write a function which will handle two forms at the same time and in same view. It is handy in many cases; we will handle more than two forms too.
Create a Django project and an app, I named the project "multipleFormHandle" and the app as "formhandlingapp".
Do some basic stuff like including app in settings.py INSTALLED_APPS and include app's url in project's url.
Now create forms.py in app and a "templates" folder in the app directory. Add home.html in templates.
Install the multi-form_view library −
pip install multi_form_view
Example
Now in urls.py of app −
from django.urls import path,include from . import views urlpatterns = [ path('',views.SchoolData.as_view(),name='home'), ]
Here, we setup views and use our viewset as view. We are going to use viewset here.
In models.py −
from django.db import models # Create your models here. class StudentData(models.Model): name=models.CharField(max_length=100) standard=models.CharField(max_length=100) section=models.CharField(max_length=100) class TeachertData(models.Model): name=models.CharField(max_length=100) ClassTeacherOF=models.CharField(max_length=100) Salary=models.CharField(max_length=100)
We created two models because we are going to save forms data in models.
In forms.py −
from django import forms from .models import TeachertData,StudentData class StudentForm(forms.ModelForm): class Meta: model=StudentData fields="__all__" class TeacherForm(forms.ModelForm): class Meta: model=TeachertData fields="__all__"
Here, we created model forms which we will render on the frontend from our view.
In home.html −
<!DOCTYPE html> <html> <head> <title>tut</title> </head> <body> <form method="post" enctype="multipart/form-data"> {% csrf_token %} <label><h3>Teacher</h3></label> //accessing form 1 from view {{ forms.teacher_form.as_p }} <label><h3>Student</h3></label> //accessing form 2 from view {{ forms.student_form.as_p }} <input type="submit" value="submit"/> </form> </body> </html>
It is our frontend where we render two Django forms under a single form element and with a single submit button.
I will not add style, because here we are learning the concept and the way to do that.
In views.py −
from django.shortcuts import render from .forms import StudentForm,TeacherForm from django.views.generic.list import ListView from django.urls import reverse from multi_form_view import MultiModelFormView # Create your views here. 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) return super(SchoolData, self).forms_valid(forms)
Here we created a viewset, we define two forms to render one is student form and the other is teacher form. We defined the HTML which we have to render. We define what to do when a form is submitted under get_success_url. In form_valid, we save the form data and verify if both the forms are right or not.