Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know which button I clicked in flask?

I my current project, my index page show several torrents with a button for each to start or stop the torrent.

I create the page with a form and a loop, so the forms have always the same names. But I need to know which button the user has clicked to know which torrent to stop !

Here is the template :

{% block content %}
 <h1>Hello, {{user}} !</h1>

 {% for torrent in torrents %}
      <form action="/index" method="post" name="torrent">
           {{torrent.control.hidden_tag()}}
           <p><a href='/torrent/{{torrent.id}}'>{{torrent.name}}</a> is {{torrent.status}} and {{torrent.progress}} % finished. <input type="submit" value="Start / Stop">
           </p>
      </form>
  {% endfor %}

Here is the view :

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

  torrents = client.get_torrents()

  # for each torrent, we include a form which will allow start or stop
  for torrent in torrents:
    torrent.control = TorrentStartStop()
    if torrent.control.validate_on_submit():
        start_stop(torrent.id)

return render_template("index.html", title = "Home", user = user, torrents = torrents)

So, how do I know which torrent the user want to stop/start ?

like image 633
22decembre Avatar asked Oct 22 '22 00:10

22decembre


1 Answers

Assuming hidden_tag creates a hidden input with name torrent_id and value the ID of the torrent, you will find the torrent id in request.form["torrent_id"]:

from flask import request

....
torrent_id = request.form["torrent_id"]
like image 178
Thomas Orozco Avatar answered Oct 24 '22 11:10

Thomas Orozco