Associate button press with table row Flask/Python Associate button press with table row Flask/Python flask flask

Associate button press with table row Flask/Python


You'll have to add the item ID into the HTML form, but you can not display it. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden

Something like the following

<form action="{{ url_for('history') }}" method="POST">    <input id="historicalId" name="historicalId" type="hidden" value="{{item.id}}">    <button type="submit"> History </button></form>

Then in flask, you'll need to parse out the request form body


I am writing this answer as it may help someone hopefully.Check the every table row is displaying a book and there is an edit button at the end of the table row.

           {% for book in books %}            <tr>                <td>{{ book[1] }}</td>                <td>{{ book[2] }} </td>                <td>{{ book[3] }} </td>                <td>{{ book[4] }} </td>                <input id="book_id" name="book_id"                       type="hidden" value="{{ book[0] }}">                <td><button type="submit" name="edit" value="{{ book[0] }}"                            formmethod="post">Edit</button></td>            </tr>

Every row has a hidden book_id which is used to retrieve the book in the backend using the below code:

        if request.form.get("edit"):        print('Book ID: ', request.form.get('edit'))