39

is it possible to submit two different forms, with one submit button in django? i have one form called "instrument" and 4 equal forms "config". now i'd like to submit always one config and instrument. e.g. instrument + config 1, and instrument + config 2. and every config have its own submit button.

i have tried it with one button in the config form:

<input onclick="submitForms()" class="btn btn-primary cfg" type="submit" value="Start" >

and call a js function 'onclick':

submitForms = function(){
    console.log('ok'); //only for testing
    document.forms["firstForm"].submit();
    document.forms["secondForm"].submit();
}

this is my method in the views.py:

if request.method == 'POST':
        form1 = dataproviderInstrumentForm(request.POST)
        form2 = dynamicTimeseriesForm(request.POST)
        print(request.POST)
        if form1.is_valid() or form2.is_valid(): 
            # do some stuff

else:
    form1 = dataproviderInstrumentForm() # an unbound form
    form2 = dynamicTimeseriesForm() # an unbound form
user2412771
  • 509
  • 2
  • 7
  • 10
  • possible duplicate of [How do I submit multiple forms with a single submit button in django?](http://stackoverflow.com/questions/15124567/how-do-i-submit-multiple-forms-with-a-single-submit-button-in-django) – Srinivas Reddy Thatiparthy Aug 28 '13 at 13:34
  • yeah i have seen this post, but isn't possible to do that without a formset only with a js function? – user2412771 Aug 28 '13 at 13:39

3 Answers3

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")
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • this looks fine if i would submit all forms at once, but i'd like to submit one config form and the instrument form at the same time. so if i submit the second config form with his own submit button the instrument form should also submit. and the submit button for the other 3 config should do the same. always submit the config form which submit button i have clicked and the instrument form. – user2412771 Aug 28 '13 at 13:48
  • what if my forms submit to different pieces of code and then gather again to single view ? – Poula Adel Apr 14 '20 at 05:07
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")
Community
  • 1
  • 1
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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '23 at 12:03