Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate button press with table row Flask/Python

I have this in index.html:

            {% for item in data %}
            <tr>
            <td>{{item[0]}}</td>
            <td>{{item[1]}}</td>
            <td>{{item[2]}}</td>
            <td>{{item[3]}}</td>
            <td>{{item[4]}}</td>
            <td>{{item[5]}}</td>
            <td>{{item[6]}}</td>
            <td>{{item[7]}}</td>
            <td>{{item[8]}}</td>
            <td>{{item[9]}}</td>
            <td>{{item[10]}}</td>
            <td>
                    <form action="{{ url_for('history') }}" method="POST">
                    <button type="submit"> History </button>
                    </form>
            </td>
            </tr>
            {% endfor %}

And this in app.py:

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

    return render_template('history.html')

So my webpage has a table with a bunch of rows and each row as a button labeled 'History'. Since right now each button does the same thing and points to history(), how can i distinguish which row of data the original click came from?

like image 434
user4784915 Avatar asked Sep 07 '25 12:09

user4784915


1 Answers

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

like image 103
OneCricketeer Avatar answered Sep 09 '25 04:09

OneCricketeer