我目前正在用烧瓶编写一个小型洪流客户端。
我的问题是,当我处理文件优先级时,我有两个要处理的文件列表,每个都包含有关不同文件的特定详细信息/命令。
而且我需要在模板中同时并行地浏览它们(这样每次我们都会谈论同一个文件!)
这是表格:
# each individual file in the torrent have its own priority, thus, we need to manage them individually !
class TorrentFileDetails(Form):
filename = HiddenField('filename')
priority = SelectField(u'File priority',choices=[('off','off'),('low','low'),('normal','normal'),('high','high')])
class TorrentForm(Form):
hidden = HiddenField('hidden')
ratiolimit = DecimalField("ratio")
downloadlimit = DecimalField("down")
uploadlimit = DecimalField("up")
bandwidthpriority = SelectField(u'Torrent priority', choices=[( -1,'low'),(0,'normal'),(1,'high')])
# we append each individual file form to this, as we don't know how many there is in each torrent !
files = FieldList(FormField(TorrentFileDetails))
这是视图部分:
# fetch informations about the torrent using the transmissionrpc python library
torrent = client.get_torrent(tor_id)
###
if torrent.seedRatioMode == 0:
torrent.seedRatioMode = 'Global ratio limit'
if torrent.seedRatioMode == 1:
torrent.seedRatioMode = 'Individual ratio limit'
if torrent.seedRatioMode == 2:
torrent.seedRatioMode = 'Unlimited seeding'
control = TorrentForm()
###
files = list()
for f in torrent.files():
fx = dict()
fx['name'] = torrent.files()[f]['name']
if torrent.files()[f]['selected'] == True:
fx['priority'] = torrent.files()[f]['priority']
else:
fx['priority'] = 0
fx['size'] = torrent.files()[f]['size']
fx['completed'] = torrent.files()[f]['completed']
files.append(fx)
f_form = TorrentFileDetails(filename=fx['name'],priority=fx['priority'])
control.files.append_entry(f_form)
if control.validate_on_submit():
type(button.data)
#start_stop_torrent(tor_id)
return render_template("torrent.html", title = torrent.name, files = files, user = user, torrent = torrent, control = control)
这是模板部分:
<h2>Files</h2>
<table class="uk-table">
<tr>
<th>File name</th>
<th>Completed / Size</th>
<th>Priority</th>
</tr>
{% for file in control.files %}
<tr>
{{file.hidden_tag()}}
<td>{{file.name}}</td> <- should come from torrent.files() list !
<td>{{file.completed|filesize}} / {{file.size|filesize}}</td> <- should come from torrent.files() list !
<td>{{file.priority}}</td>
{# <td><button type="submit">Send</button></td> #}
</tr>
{% endfor %}
</table>
正如你所看到的,我有一个列表是那些来自传输并且只包含名称、大小和基本内容的列表。这些不是要控制的,所以不是形式。另一个列表是表单本身,它包含控件,但不能只显示文件大小或其他。
所以在模板中,我应该相应地遵循两个列表,但不知道如何!
有人有想法吗?建议 ?
谢谢 !