【发布时间】:2011-04-16 03:35:20
【问题描述】:
我想刷新 Django 中包含温度数据的 div 标签。每 20 秒获取一次数据。到目前为止,我已经使用这些功能实现了这一点:
function refresh() {
$.ajax({
url: '{% url monitor-test %}',
success: function(data) {
$('#test').html(data);
}
});
};
$(function(){
refresh();
var int = setInterval("refresh()", 10000);
});
这是我的 urls.py:
urlpatterns += patterns('toolbox.monitor.views',
url(r'^monitor-test/$', 'temperature', name="monitor-test"),
url(r'^monitor/$', 'test', name="monitor"),
)
views.py:
def temperature(request):
temperature_dict = {}
for filter_device in TemperatureDevices.objects.all():
get_objects = TemperatureData.objects.filter(Device=filter_device)
current_object = get_objects.latest('Date')
current_data = current_object.Data
temperature_dict[filter_device] = current_data
return render_to_response('temp.html', {'temperature': temperature_dict})
temp.html 有一个包含标签:
<table id="test"><tbody>
<tr>
{% include "testing.html" %}
</tr>
</tbody></table>
testing.html 只包含一个用于遍历字典的 for 标签:
{% for label, value in temperature.items %}
<td >{{ label }}</td>
<td>{{ value }}</td>
{% endfor %}
div 每 10 秒刷新一次,允许我使用模板系统而无需使用 js 对其进行修补。但是,几分钟后,我同时收到 3-4 次重复呼叫“/monitor-test”。另外,我想知道是否有更好的方法来做到这一点,同时能够在 Django 中使用模板系统。谢谢。
【问题讨论】:
标签: django jquery django-views