0

I have a website where I gave 2 different forms. Here is my HTML:

{% extends 'base.html' %}
{% block title %} 
Home page
{% endblock %}
{% block content %}
<div class="pt-5">
<form action="data" method="POST" enctype="multipart/form-data">
   <input type="file" name="file" accept=".csv, .xlsx, .xls" />
   <input type="submit" name="form1" value="Submit"/>
</form>
</div>
<div class="pt-5">
   <form action="data" method="POST" enctype="multipart/form-data">
      <input type="file" name="file1" accept=".csv, .xlsx, .xls" />
      <input type="file" name="file2" accept=".csv, .xlsx, .xls" />
      <input type="submit" name="form2" value="Merge"/>
   </form>
</div>

Both forms uploads files, the first form uploads just one file while the other form should Merge two files. Now both will actually to the same page. How can I differentiate between them? I searched and I have come across solutions like this:

if request.method == "POST":
    if "form1" in request.form:
        print("form1")
    elif "form2" in request.form:
        print("form2")

But this does not work for me, how can I know from which form my request is coming from? Thanks.

EDIT

The solution I provided above works now, I did not know why it did not work before...

Mohammed Abdu
  • 25
  • 2
  • 9
  • I think You have to specify the `action` parameter in html and then have it redirect to the same route but with some kind of a variable that way You can differentiate between forms – Matiiss Jul 21 '21 at 12:06
  • it would be easier if you create two post routes for forms, like for form 1 `
    ` and form 2 ``
    ` and access the file as `request.form.get('file1')` and so on
    – Epsi95 Jul 21 '21 at 12:10
  • Isnt there a better way to do so? something like the soultion I did above but did not work. This does not seem so effective.. – Mohammed Abdu Jul 21 '21 at 12:12
  • @Epsi95 but then I do have two different routes data1 and data2. Can't I just have the same route and still achieve it? It feels like Flask must have done something to acheive what I am looking for without createing many routes. What if I have 5-6 forms in one page. Then I need a new route for each form? not so effective in my opinion.. – Mohammed Abdu Jul 21 '21 at 12:15
  • there is nothing fancy here, whenever you are making form, under the hood it is just `post`ing some data to the specific url, the best you can do is to create one hidden fileld in your form, and in your flask you can check which form it to extract values like `` and keep the route same. Then in your code you can do like `request.form.get('formid') == '1')`@MohammedAbdu – Epsi95 Jul 21 '21 at 12:18
  • @Epsi95 I will try it out – Mohammed Abdu Jul 21 '21 at 12:29
  • @MohammedAbdu check some of the responses in [this SO question](https://stackoverflow.com/questions/18290142/multiple-forms-in-a-single-page-using-flask-and-wtforms) – Gitau Harrison Jun 15 '22 at 14:02

1 Answers1

0

In my opinion, using different attribute action is easier like below.

<form action="upload_1" method="post"> ... </form>
Darcy
  • 160
  • 1
  • 9