我需要顯示來自HttpResponse的數據作爲圖表(xaxis = datetime,yaxis是一個聚合統計函數)。我成功地將數據導出到一個csv文件。如何將django與amcharts集成?amcharts與django的集成
def monitor(request):
if request.method == 'POST' :
forms = ServicesForm(request.POST)
if forms.is_valid():
service = forms.cleaned_data['service']
scale = forms.cleaned_data['scale']
datatype = forms.cleaned_data['datatype']
starttime = forms.cleaned_data['starttime']
endtime = forms.cleaned_data['endtime']
db_table = u'servicestats'
name = Servicenames.objects.get(servicename=service)
servicestats = Servicestats.objects.raw('SELECT distinct dtime,scale,' + datatype + ' FROM ' + db_table + ' WHERE scale = ' + str(scale) + ' AND dtime BETWEEN "' + str(starttime) + '" AND "' + str(endtime) + '" AND service_id = ' + str(name.id))
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment;filename="export.csv"'
writer = csv.writer(response)
for s in servicestats:
if datatype =='cnt':
writer.writerow([s.dtime,s.cnt])
if datatype =='avg' :
writer.writerow([s.dtime,s.avg])
if datatype =='min' :
writer.writerow([s.dtime,s.min])
if datatype =='max' :
writer.writerow([s.dtime,s.max])
if datatype =='threenines' :
writer.writerow([s.dtime,s.threenines])
return response
else:
forms = ServicesForm
return render_to_response('monitor/monitor.html', {'forms':forms},
context_instance = RequestContext(request))
template,html :
<form action='/monitor/' method="post"> {%csrf_token%}
{{ forms.as_p}}
<input type = "submit" value = "Submit">
</form>
我不明白你的問題。它看起來像你的監視器視圖旨在表現爲一個寧靜的界面,其中表單的帖子返回一個CSV文件?你的情況怎麼樣? –
這是我的問題。我返回了一個csv文件來測試我的查詢。我需要返回amchart圖。 – dave