刷新表Dajaxice在Django [英] Refreshing table with Dajaxice in Django

查看:130
本文介绍了刷新表Dajaxice在Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我监视不同位置的温度。我有存储在一个模型中的数据,并把我的views.py,但我想刷新表每5分钟。我是新来的Ajax和dajaxice,我怎么能写功能,因此显示在HTML中?这是我的views.py:

I'm monitoring the temperature for different locations. I have the data stored in a model and have set my views.py, but I would like to refresh the table every 5 minutes. I'm new to ajax and dajaxice, how can I write the function so it displays in html? this is my 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})

至于什么,我想我了解,到目前为止,这可能是我的ajax.py,我应该只是修改它返回一个simplejson转储。如果我错了,请大家指正。这是我的temp.html:

As for what I think I'm understanding so far, this could be my ajax.py, I should just modify it to return a simplejson dump. Please correct me if Im wrong. This is my temp.html:

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

下面是我卡住。我怎么能写这篇文章,使我的回调刷新表?

Here is where I get stuck. How can I write this so that my callback refreshes the 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回调,分配'表'到你的HTML容器......(这只是一个例子):

And as JS callback, assign 'table' to your html container... (It's only an example):

function my_callback(data){
    if(data!=Dajaxice.EXCEPTION){
        document.getElementById('your_table_container_id').innerHTML = data.table;
    }
    else{
        alert('Error');
    }
}

希望这有助于你。

Hope this help you.

这篇关于刷新表Dajaxice在Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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