我目前正在写一个带有烧瓶的小型torrent客户端。
我的问题是,当我处理文件优先级时,我有两个要处理的文件列表,每个文件都包含有关不同文件的特定详细信息/命令。
我需要同时并行地在模板中浏览它们(所以每次我们都谈论相同的文件!)
以下是表格:
# 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>
正如你所看到的,我有一个列表,它来自传输,只包含名称,大小,基本内容。这些不是控制因此,而不是形式。另一个列表是表单本身,它包含控件,但不能只显示文件大小或其他。
所以在模板中,我应该相应地遵循这两个列表,但不知道如何!
有人有想法吗?建议?
谢谢!
答案 0 :(得分:0)
通过在表单中使用HiddenField传递我需要的文本来解决。
例如,文件名和大小变为隐藏字段:
filename = HiddenField('filename')
size = HiddenField('size')
之后,我不再需要fx dict,也不需要files()列表。 在模板中,文本片段的值为:
<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.filename.data}}</td>
<td>{{file.completed.data}} / {{file.size.data}}</td>
<td>{{file.priority}}</td>
{# <td><button type="submit">Send</button></td> #}
</tr>
{% endfor %}
</table>