django submit two different forms with one submit button

Instead of having multiple <form ..> tags in html, use only one <form> tag and add fields of all forms under it.

Example in template

<form >
    {{ form1.as_p }}
    {{ form2.as_p }}
    {{ form3.as_p }}
</form>

So when user submits the form you will get all forms data in view, then you can do what you are doing in view. As

if request.method == 'POST':
        form1 = Form1(request.POST)
        form2 = Form2(request.POST)
        print(request.POST)
        if form1.is_valid() or form2.is_valid(): 

Its better to use form prefix in such cases.

So you can do

if request.method == 'POST':
        form1 = Form1( request.POST,prefix="form1")
        form2 = Form2( request.POST,prefix="form2")
        print(request.POST)
        if form1.is_valid() or form2.is_valid(): 
else:
        form1 = Form1(prefix="form1")
        form2 = Form2(prefix="form2")

Leave a Comment