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

4
  • You probably have to write some JS to handle this html forms. Check out this link: developer.mozilla.org/en-US/docs/Learn/Forms/… why do you need two forms?The whole approach seems weird and not the best to do. Commented Apr 28, 2022 at 10:42
  • Sure whatever, here another link: stackoverflow.com/a/1395866/10873394 Approach doesn't change with simple HTML form you just write JS handling code Commented Apr 28, 2022 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? Commented Apr 28, 2022 at 10:57
  • @IainShelvington am submitting one form and needs to determine which was submitted in view Commented Apr 28, 2022 at 11:16

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.