如何使用此字典在javascript模板中显示文件信息?
我有一本这样的字典,我已经在Django中传递给模板:如何使用此字典在javascript模板中显示文件信息?,javascript,django,dictionary,Javascript,Django,Dictionary,我有一本这样的字典,我已经在Django中传递给模板: files = { "20090209.02s1.1_sequence.txt": [645045714, 2584.9807732105255, 137625600], "20090209.02s1.2_sequence.txt": [645045714, 2569.1707730293274, 158859264] } 键是文件名,值是数组。数组中的第一个元素由文件总大小、第二个元素->持续时间文件传输已开始和第三个
files = {
"20090209.02s1.1_sequence.txt": [645045714, 2584.9807732105255, 137625600],
"20090209.02s1.2_sequence.txt": [645045714, 2569.1707730293274, 158859264]
}
键是文件名,值是数组。数组中的第一个元素由文件总大小、第二个元素->持续时间
文件传输已开始和第三个元素->传输的大小数
组成
我在表中列出了文件信息以及传输文件的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 %}
如何提取字典数据并将信息放入模板中相应的文件列中?感谢使用ajax调用:
说出你的ajax\u template.html
:
{% 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 %}
现在在ajax中执行以下操作:
$.ajax({
url:'/view/',
success:function(result){
$('#div').html('').html(result)
}})
其中,您的$(“#div”)是html div in template,您要在其中呈现结果为什么不使用ajax调用从服务器获取呈现的模板..是的,我使用ajax调用每5秒从服务器获取一次信息。是的,对。。因此,您需要做的是以json(或其他格式)获取响应。。。作为响应,给出html。。像
def视图(request):if request.is_ajax():return render('ajax_template.html',locals())
。。在ajax成功函数中,将html替换为响应html,这样您就可以仅在服务器端的后端呈现数据。。其中,您的ajax_模板就是您上面编写的模板代码。$(“#div”).load(“/view/”)
?$(“#div”).load(“/view/”)我认为是错误的。。我应该是$(“#div”).load(#一些过程)您可以处理响应,但是jquery文档说:Description:从服务器加载数据,并将返回的HTML放入匹配的元素中。
def view(request):
if request.is_ajax():
# this view get called at '/view/' url
#do your calculations here instead in javascript function
# in case your fetching some variables from client side then send them in POST or GET ..
return render('ajax_template', locals())
$.ajax({
url:'/view/',
success:function(result){
$('#div').html('').html(result)
}})