I want to update a div tag in Django that contains temperature data. Data is retrieved every 20 seconds. So far, I have achieved this using the following functions:
function refresh() { $.ajax({ url: '{% url monitor-test %}', success: function(data) { $('#test').html(data); } }); }; $(function(){ refresh(); var int = setInterval("refresh()", 10000); });
And this is my 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 has an include tag:
<table id="test"><tbody> <tr> {% include "testing.html" %} </tr> </tbody></table>
test.html just contains a for tag to iterate through a dictionary:
{% for label, value in temperature.items %} <td >{{ label }}</td> <td>{{ value }}</td> {% endfor %}
The div is updated every 10 seconds and allows me to use the template system without fixing it with js. However, I get repeated calls to '/ monitor-test', 3-4 at the same time after a couple of minutes. Also, I was wondering if there is a better way to do this while being able to use the template system in Django. Thanks.
source share