我有一本这样的字典,我已将其传递给 Django 中的模板:
files = {
"20090209.02s1.1_sequence.txt": [645045714, 2584.9807732105255, 137625600],
"20090209.02s1.2_sequence.txt": [645045714, 2569.1707730293274, 158859264]
}
键是文件名,值是数组。数组中的第一个元素由total_file_size, second_element -> duration
文件传输开始和third element -> number of size transferred
.
我在表格中列出了文件信息以及 remPercentage 和 remTime 以传输文件。
{% for choice in overview %}
<tr>
<td><input type="checkbox" name="choice_stop" id="choice{{ forloop.counter }}" value="{{ choice.id }}" onchange="checkChecked_stop()"/></td>
<td><label for="choice{{ forloop.counter }}">{{ choice.name }}</label></td>
<td>{{ choice.source }}</td>
<td><div id="remPercentage{{ forloop.counter }}"></div></td>
<td>{{ choice.start_date }}</td>
<td><div id="remTime{{ forloop.counter }}"></div></td>
<td>{{ choice.human_size }}</td>
<td><input type="checkbox" disabled {{ choice.flag_email }} /></td>
</tr>
{% endfor %}
这是我的名为 changeData() 的 Javascript 函数,它正在访问服务器并获取remPercentage
and remTime
。
var interval;
var counter=0;
var length;
var speed;
var duration;
function changeData() {
var url="/ajax_call_size";
var xmlh=new XMLHttpRequest();
xmlh.open("GET",url,false);
xmlh.send(null);
var resObj=xmlh.responseText;
var total_size = get the total_size
var transfer_duration = get the transfer_duration
var transfer_size = get the transfer_size
length=total_size.length;
for(var i=0;i<length;i++) {
if(typeof(transfer_size[i])!="undefined") {
transfer_per = parseInt(100-(transfer_size[i]/total_size[i] * 100));
remaining_size = total_size[i] - transfer_size[i];
if(transfer_per!=0) {
eTime_remaining = (transfer_duration[i]/transfer_size[i])*(total_size[i]-transfer_size[i]) ;
document.getElementById("remPercentage"+parseInt(i+1)).innerHTML=transfer_per+"%";;
document.getElementById("remTime"+parseInt(i+1)).innerHTML=secondsToString(eTime_remaining);
} else {
document.getElementById("remPercentage"+parseInt(i+1)).innerHTML="1%";
document.getElementById("remTime"+parseInt(i+1)).innerHTML="1 sec";
}
} else {
document.getElementById("remPercentage"+parseInt(i+1)).innerHTML="completed";
document.getElementById("remTime"+parseInt(i+1)).innerHTML="completed";
}
}
counter+=1;
}
setInterval("changeData()",5000);
如何提取字典数据并将信息放在模板中的相应文件列中?谢谢