如何知道我点击了烧瓶中的哪个按钮?

22d*_*bre 8 python flask wtforms flask-wtforms

在我当前的项目中,我的索引页面显示了几个 torrent,每个 torrent 都有一个启动或停止 torrent 的按钮。

我使用表单和循环创建页面,因此表单始终具有相同的名称。但我需要知道用户单击了哪个按钮才能知道要停止哪个 torrent!

这是模板:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

这是视图:

  @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)
Run Code Online (Sandbox Code Playgroud)

那么,我如何知道用户想要停止/启动哪个种子?

Tho*_*zco 5

假设hidden_tag创建一个hidden带有名称torrent_id和值的输入 torrent ID,您将在以下位置找到 torrent id request.form["torrent_id"]

from flask import request

....
torrent_id = request.form["torrent_id"]
Run Code Online (Sandbox Code Playgroud)


jhe*_*ann 0

使用 HTML<button type="submit">元素并将 设为value信息哈希,或设置formaction包含信息哈希的 URL(formaction仅限 HTML5)。