【发布时间】:2013-09-28 14:11:07
【问题描述】:
我当前的项目,我的索引页面显示了几个种子,每个种子都有一个按钮来启动或停止种子。
我使用表单和循环创建页面,因此表单始终具有相同的名称。但我需要知道用户点击了哪个按钮才能知道要停止哪个种子!
这是模板:
{% 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)
那么,我如何知道用户想要停止/启动哪个种子?
【问题讨论】:
-
我们能看到
hidden_tag的代码吗? -
Thomas 是对的,因为您为每个种子打开一个表单,所以整个问题真的不存在,因为您可以简单地在隐藏字段中提交信息哈希。我的回答假定整个页面或列表使用单一形式。
标签: python flask wtforms flask-wtforms