5

私の現在のプロジェクトでは、インデックス ページにいくつかの 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 %}

ビューは次のとおりです。

  @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)

では、ユーザーが停止/開始したいトレントをどのように知ることができますか?

4

3 に答える 3

4

名前と値が torrent の ID である入力をhidden_tag作成すると仮定すると、torrent id は次の場所にあります。hiddentorrent_idrequest.form["torrent_id"]

from flask import request

....
torrent_id = request.form["torrent_id"]
于 2013-09-28T14:38:17.403 に答える
0

HTML<button type="submit">要素を使用しvalueて、 を情報ハッシュに設定するか、情報ハッシュformactionを含む URL を設定します ( formactionHTML5 のみ)。

于 2013-09-28T14:37:06.870 に答える