Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django submit two different forms with one submit button

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 
like image 244
user2412771 Avatar asked Aug 28 '13 13:08

user2412771


People also ask

How to submit 2 forms with one button django?

Instead of having multiple <form ..> tags in html, use only one <form> tag and add fields of all forms under it. if request. method == 'POST': form1 = Form1(request. POST) form2 = Form2(request.

How do I stop multiple form submissions in Django?

Multiple form submission happens because when page refreshes that same url hits, which call that same view again and again and hence multiple entries saved in database. To prevent this, we are required to redirect the response to the new url/view, so that next time page refreshes it will hit that new url/view.

What is formset in Django?

A formset is a layer of abstraction to work with multiple forms on the same page. It can be best compared to a data grid. Let's say you have the following form: >>> from django import forms >>> class ArticleForm(forms.


2 Answers

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") 
like image 54
Rohan Avatar answered Oct 12 '22 00:10

Rohan


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") 
like image 27
Muhammad Faizan Fareed Avatar answered Oct 12 '22 02:10

Muhammad Faizan Fareed