I want the chart refresh itself(not to refresh the whole web page) ,like a heart monitor ,everytime a new data has been added to the database,is that possible?
With codes below,i must refresh the web page and see if there's any new data.
views.py:
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())
chart.html
var chartData = {{chartData}}
chart.dataProvider = chartData;