2013-09-28 120 views
3

我目前的项目,我的索引页面显示了几个种子,每个种子都有一个按钮,用于启动或停止洪流。如何知道我在烧瓶中点击了哪个按钮?

我使用窗体和循环创建页面,因此窗体始终具有相同的名称。但我需要知道用户点击了哪个按钮才能知道哪个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) 

所以,我怎么知道哪个洪流用户想要停止/启动?

+0

我们可以看到'hidden_​​tag'的代码吗? –

+0

托马斯是正确的,因为你打开一个表格每个洪流整个问题是真的不存在,因为你可以简单地提交信息哈希在一个隐藏的领域。我的答案假设整个页面或列表的单一表单。 – jhermann

回答

0

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

2

假设hidden_tag创建hidden输入名称为torrent_id和价值洪流的ID,你会发现洪流ID在request.form["torrent_id"]

from flask import request 

.... 
torrent_id = request.form["torrent_id"] 
+0

hidden_​​tag只是一个csrf保护标记。但我会尝试使用你的建议。 – 22decembre

+0

@ 22decembre只需用''替换'a',你就会很好。 –

+0

好的,这解决了“哪个按钮被点击”的问题,顺便说一下,我了解了我不知道的按钮(我知道输入,而不是按钮)。 但是现在出现这样一个事实,即我得到尽可能多的答案,因为我已经因为循环而内联了!那么我如何才能启动一次函数呢? – 22decembre

0

托马斯给出了正确的答案!

现在来了一个事实,即我得到尽可能多的答案,因为我有内联,因为循环!那么我如何才能启动一次函数呢?

我知道这听起来很愚蠢,但让我们说我没有看到它!

+0

通过将'if torrent.control.validate_on_submit'退出循环解决。 – 22decembre

相关问题