我正在监测不同地点的温度。我将数据存储在模型中并设置了views.py,但我想每 5 分钟刷新一次表。我是 ajax 和 dajaxice 的新手,如何编写该函数以使其显示在 html 中?这是我的观点.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})
至于我认为到目前为止我所理解的,这可能是我的 ajax.py,我应该修改它以返回 simplejson 转储。如果我错了,请纠正我。这是我的 temp.html:
<table id="tval"><tr>
{% for label, value in temperature.items %}
<td>{{ label }}</td>
<td>{{ value }}</td>
{% endfor %}
</tr></table>
这就是我被困住的地方。我该如何编写这个以便我的回调刷新表?
使用类似的东西:
from django.template.loader import render_to_string
from django.utils import simplejson
from dajaxice.core import dajaxice_functions
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
table = render_to_string('temp.html', {'temperature': temperature_dict})
return simplejson.dumps({'table':table})
dajaxice_functions.register(temperature)
作为 JS 回调,将“table”分配给您的 html 容器...(这只是一个示例):
function my_callback(data){
if(data!=Dajaxice.EXCEPTION){
document.getElementById('your_table_container_id').innerHTML = data.table;
}
else{
alert('Error');
}
}
希望这对您有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)