0

In flask function, how to I get where the POST request is from? From which .html or which form?

I have a function in .py

@app.route("/mark_read", methods=['GET', 'POST'])
def mark_read():

so when it gets a POST request, it will extract data and delete corresponding rows in my .db file.

I have multiple .html and many of them have a delete button, instead of write many function to deal with each single case, I want to just use def mark_read(): to deal with all delete request. But how do I know where the delete is from so I can go to the correct table in my .db to perform deletion. The extracted data is not representative so I cannot use extracted data to decide the appropriate table?

For example, we can use

identifier = request.form['set_mark']

to get the value of an element with name set_mark, is there a similar way to get which form it is? Maybe something like:

<form action="{{ url_for('mark_read') }}" method="POST" name="form_name" value="form1">

then I can get which form is it by

identifier = request.form['form_name'] # form1
Kenny Wang
  • 69
  • 1
  • 7
  • Browser should send request with header [referer](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer) - it should be url of page which send request. – furas Apr 03 '19 at 21:34
  • [request.referrer](http://flask.pocoo.org/docs/1.0/api/#flask.Request.referrer). Or you can use `` in form. You can also use arguments in url `action=url?arg=value` like in `GET` request. – furas Apr 03 '19 at 21:52
  • Thank! Actually I found an answer right now [see this post](https://stackoverflow.com/questions/26217779/how-to-get-the-name-of-a-submitted-form-in-flask) – Kenny Wang Apr 03 '19 at 21:58

0 Answers0