Solution 1 :

if you want to keep the two separate forms:

if request.method == "POST" and "selectgenderform" in request.POST:

if request.method == "POST" and "selectionform" in request.POST:

you might also have to change the submit input names to “selectgenderform” and “selectionform”

Solution 2 :

You can pass multiple forms and handle it using single function. However the trick is to keep the forms under one form tag in template. (Bear with me as I am typing on my phone)

def yourView(request):
    form1 = form1()
    form2 = form2()

    if request.method == "post":
        form1 = form1(request.POST)
        form2 = form2(request.POST)

        if form1.is_valid():
            #do something
        if form2.is_valid():
            #do something else 
   contest = { "form1": form1, "form2": form2 }
   return render(request, 'template.html', context=context)


    <form method="POST">
    <button type="submit"> Submit </button>

Problem :

I have to handle two forms in one function:

HTML page

<form name="selectgenderform" method = "POST">
  <select name='gender'>
 <input type = 'submit' name ='searchbtn' value= 'search' >

<form name="selectionform" method = "POST">
  <input type = 'hidden' name = 'valueofnumber' >
 <input type = 'submit' name = 'searchbtn' value= 'search' >

 def formselection(request):
   if selectgenderform in request.POST:
     gender = request.POST.get('gender')

   elif selectionform in request.POST:
     value = request.POST.get('valueofnumber') 

My query is to handle multiple forms in one function but this will not according to my demand


Comment posted by Mahad_Akbar

Thank you for your answer, but with this one first I have to make forms of table from ?

Comment posted by k33da_the_bug

@Mahad_Akbar Not necessary. You can handle it manually too, but this one using forms is little bit cleaner approach.

Comment posted by Mahad_Akbar

Kindly can you explain it with manually, I am try it for long but this was not working