我想在Django中刷新包含温度数据的div标签.每20秒获取一次数据.到目前为止,我已使用以下功能实现了这一目
function refresh() {
$.ajax({
url: '{% url monitor-test %}',
success: function(data) {
$('#test').html(data);
}
});
};
$(function(){
refresh();
var int = setInterval("refresh()", 10000);
});
Run Code Online (Sandbox Code Playgroud)
这是我的urls.py:
urlpatterns += patterns('toolbox.monitor.views',
url(r'^monitor-test/$', 'temperature', name="monitor-test"),
url(r'^monitor/$', 'test', name="monitor"),
)
Run Code Online (Sandbox Code Playgroud)
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})
Run Code Online (Sandbox Code Playgroud)
temp.html有一个include标记:
<table id="test"><tbody>
<tr>
{% include "testing.html" %}
</tr>
</tbody></table>
Run Code Online (Sandbox Code Playgroud)
testing.html只包含一个for标签来遍历字典:
{% for label, value in temperature.items %} …Run Code Online (Sandbox Code Playgroud) 我正在监测不同位置的温度.我将数据存储在模型中并设置了我的views.py,但我想每隔5分钟刷新一次表.我是ajax和dajaxice的新手,如何编写函数以便在html中显示?这是我的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})
Run Code Online (Sandbox Code Playgroud)
至于我认为到目前为止我所理解的,这可能是我的ajax.py,我应该修改它以返回一个simplejson转储.如果我错了,请纠正我.这是我的temp.html:
<table id="tval"><tr>
{% for label, value in temperature.items %}
<td>{{ label }}</td>
<td>{{ value }}</td>
{% endfor %}
</tr></table>
Run Code Online (Sandbox Code Playgroud)
这是我被卡住的地方.我怎么写这个,以便我的回调刷新表?