使用模板系统在Django中使用JQuery刷新div [英] Refresh div using JQuery in Django while using the template system

查看:102
本文介绍了使用模板系统在Django中使用JQuery刷新div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想刷新包含温度数据的django。数据每20秒读取一次。到目前为止,我已经使用这些功能实现了这一点:

I want to refresh a in django that contains temperature data. The data is fetched every 20 seconds. So far I have achieved this using these functions:

function refresh() {
$.ajax({
  url: '{% url monitor-test %}',
  success: function(data) {
  $('#test').html(data);
  }
});
};
$(function(){
    refresh();
    var int = setInterval("refresh()", 10000);
});

这是我的urls.py:

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有一个包含标签:

temp.html has an include tag:

<table id="test"><tbody>
<tr>
{% include "testing.html" %}
</tr>
</tbody></table>

testing.html只包含一个用于迭代字典的标签:

testing.html just contains a for tag to iterate through the dictionary:

{% for label, value in temperature.items %}
      <td >{{ label }}</td>
      <td>{{ value }}</td>
{% endfor %}

div每10秒刷新一次,并允许我使用模板系统没有使用js进行修补。但是,在几分钟后,我再次打电话给/ monitor-test,同时3-4点。此外,我想知道是否有更好的方式来做,而可以使用Django中的模板系统。谢谢

The div is refreshed every 10 seconds and allows me to use the template system without patching 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

推荐答案

我通常会遇到这种情况的3-4个并发请求的方式是将 setInterval()在函数内调用我想重复运行。

The way I normally get around the 3-4 "concurrent" requests for such situations is to put the setInterval() call inside the function I want to run repeatedly.

function refresh() {
    $.ajax({
        url: '{% url monitor-test %}',
        success: function(data) {
            $('#test').html(data);
        }
    });
    setInterval("refresh()", 10000);
}

$(function(){
    refresh();
});

每次刷新函数被调用,它将自动设置为在10秒内再次调用。另一个想法(如果你还有问题)是将 setInterval 移动到AJAX调用中的成功函数中:

That makes it so every time the refresh function is called, it will automatically set itself to be called again in 10 seconds. Another idea (if you still have problems) is to move the setInterval into the success function in the AJAX call:

function refresh() {
    $.ajax({
        url: '{% url monitor-test %}',
        success: function(data) {
            $('#test').html(data);
        }
        setInterval("refresh()", 10000);
    });
}

$(function(){
    refresh();
});

如果由于某种原因,AJAX调用不成功,该选项可能有点粗略。但是,您可以随时使用其他处理程序来处理这些问题,我想...

That option might be a little bit sketchy if, for whatever reason, the AJAX call does not succeed. But you can always get around that with other handlers, I suppose...

我有一个建议(与您的问题无关)将整个< table> 在您呈现的模板中的东西,并返回您的温度视图。所以,在你的主模板中:

One suggestion I have (not particularly related to your question) is putting the whole <table> thing in the template that you render and return your temperature view. So, in your main template:

<div id="test">
{% include 'testing.html' %}
</div>

testing.html p>

and in testing.html:

<table><tr>
{% for label, value in temperature.items %}
    <td>{{ label }}</td>
    <td>{{ value }}</td>
{% endfor %}
</tr></table>

关于以当前拥有的方式插入表格的一些东西,让我想哭泣:)发送AJAX电话中的电线几个字节不应该伤害任何东西。

Something about inserting part of a table the way you currently have it makes me want to cry :) Sending a few more bytes over the wire in AJAX calls shouldn't hurt anything.

这篇关于使用模板系统在Django中使用JQuery刷新div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆