0

I have two simple HTML forms on one page. I want to create Django view to submit multiple Django forms. I can submit form1 but I have no clue how to submit form2.

There is lot of material on internet but they are all about Djanog forms. Please help me with HTML form submission.

HTML Form

<form action="" method=post name="form1" id="form1">
<input type="text" id="input_form1" name="input_form1">
<button type="submit">Submit</button>         
</form>

<form action="" method=post name="form2" id="form2">
<input type="text" id="form2" name="form2">
<button type="submit">Submit</button>         
</form>

Views.py

def index(request):
  if request.method == 'POST':
     input_form1 = request.POST.get('input_form1')
  return render(request, 'index.html', params)

Please elaborate how to integrate form2 in Views.py

CodePak
  • 93
  • 1
  • 12
  • You probably have to write some JS to handle this html forms. Check out this link: https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_forms_through_JavaScript why do you need two forms?The whole approach seems weird and not the best to do. – Michał Darowny Apr 28 '22 at 10:42
  • Sure whatever, here another link: https://stackoverflow.com/a/1395866/10873394 Approach doesn't change with simple HTML form you just write JS handling code – Michał Darowny Apr 28 '22 at 10:50
  • Are you submitting both forms at once or are you submitting one form and need to determine which was submitted in the view? – Iain Shelvington Apr 28 '22 at 10:57
  • @IainShelvington am submitting one form and needs to determine which was submitted in view – CodePak Apr 28 '22 at 11:16

1 Answers1

1

you can put hidden input inside of each form to identify them

index.html

<form action="" method="post">
    {% csrf_token %}
    <input type="text" id="input_form1" name="input_form1">
    <button type="submit">Submit</button>         
    <input type="hidden" name="which_form_is_it" value="this_is_form_1">
</form>


<form action="" method="post">
    {% csrf_token %}
    <input type="text" id="input_form2" name="form2">
    <button type="submit">Submit</button>  
    <input type="hidden" name="which_form_is_it" value="this_is_form_2">       
</form>

views.py

def index(request):
    if request.method == 'POST':
        #watch output in console
        print(request.POST)
        which_form_is_submiting = request.POST["which_form_is_it"]
        
        if str(which_form_is_submiting) == "this_is_form_1":
            #here is yor logic for data from form 1
            form_1_input = request.POST["input_form1"]
        if str(which_form_is_submiting) == "this_is_form_2":
            #here is your logic for data from form 2
            form_2_input = request.POST["input_form2"]
        return render(request, 'index.html', params)
oruchkin
  • 1,145
  • 1
  • 10
  • 21
  • I have made few adjustment and now it works Alhamdulillah...I have added an empty dict on top which_form_is_submiting = [] then use try and except to avoid key error try: which_form_is_submiting = request.POST["type_of_form"] except: KeyError and then the whole code above...thanks a lot – CodePak Apr 30 '22 at 10:13