Refreshing table with Dajaxice in Django

时光毁灭记忆、已成空白 提交于 2019-12-04 17:13:37

Use something similar like this:

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!