[Django]-Django submit two different forms with one submit button

80👍

✅

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

4👍

Extending the @Rohan answer and adding more control on forms.

Not dependent forms/Without relationship/Save any form from multiple forms

Check individually each form to check which form are not valid. Then store them into context if contain errors or redirect them.

if request.method == 'POST':
    form1 = Form1( request.POST,prefix="form1")
    form2 = Form2( request.POST,prefix="form2")
    
    if form1.is_valid():
       # save them    
       
       # context['form1_message'] = 'Form1 saved'
    else: 
       #save them into context
       context['form1']= form1
    
    if form2.is_valid():
       # save them    
       # context['form2_message'] = 'Form2 saved'
    else: 
       #save them into context
       context['form2']= form2

    if form1.is_valid() and  form2.is_valid(): 
       #that's mean both form is valid and saved successfully 
       return redirect('page')
    else:
        return render('/page', context)


else:
    form1 = Form1(prefix="form1")
    form2 = Form2(prefix="form2")

Dependent forms/Modelform(1-1,1-m)/Relationship form

One Parent form and one child form that depends on Parent form. if both forms are saved or checked errors at same time then we will use this method.

if request.method == 'POST':
    form1 = Form1( request.POST,prefix="form1")
    form2 = Form2( request.POST,prefix="form2")
    
    if not form1.is_valid():
       #save them into context
       context['form1']= form1
    
    if not form2.is_valid():
       #save them into context
       context['form2']= form2

    if form1.is_valid() and  form2.is_valid(): 
       #that's mean both form is valid and saved successfully 
       return redirect('page')
    else:
        return render('/page', context)


else:
    form1 = Form1(prefix="form1")
    form2 = Form2(prefix="form2")

0👍

i didnt get much luck with the above but ive been using crispy forms. So the way i did it finally i had different forms but in crispy forms i set the self.helper.form_tag = False, that way i only carry the Html form tags and the rest eventhough are forms and i can specify it in html i can submit all data together

Leave a comment