私の現在のプロジェクトでは、インデックス ページにいくつかの 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)
では、ユーザーが停止/開始したいトレントをどのように知ることができますか?