我想要图表刷新自身(而不刷新整个网页),就像心脏监视器一样,每次将新数据添加到数据库中,这可能吗?
使用以下代码,我必须刷新网页并查看是否有任何新数据。
from tempsensors.models import temp
def temp_page(request):
#this view is to store the temperature value into my database
#data is send by my board with a temperature sensor
if request.method == 'POST' and 'tem' in request.POST and request.POST['tem']:
temp_value = float(request.POST['tem'])
temp_add = temp(temp=temp_value)
temp_add.save()
else:
#only for post data
raise Http404
return render_to_response("temp.html",locals(),context_instance=RequestContext(request))
def chart_page(request):
#this view will display a chart
temps = temp.objects.all()
# formatting JSON file
prefix = ''
chartData = "["
for t in temps:
chartData += prefix
chartData += "{\n"
chartData += " date: "
chartData += "new Date(" + str(t.temp_date.year) + ","
chartData += str(t.temp_date.month-1) + ","
chartData += str(t.temp_date.day) + ","
chartData += str(t.temp_date.hour) + ","
chartData += str(t.temp_date.minute) + "),\n"
chartData += " value: "
chartData += str(t.temp) + "\n }"
prefix = ", "
chartData += "]"
temp_now = temp.objects.order_by("-id")[0].temp
return render_to_response('chart.html', locals())
var chartData = {{chartData}}
chart.dataProvider = chartData;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句