Django.I的新手。我从mysql db中检索显示到表单中的数据。用户需要在表单选项中进行选择。该视图通过查询数据库处理从表单发布的数据,并应以csv格式或图形形式发送结果。我现在正在尝试使用查询结果创建csv文件,但提交不起作用。 views.py:
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']
id = Servicenames.objects.raw('SELECT id FROM servicenames WHERE servicename = ' + service )
# process and create query Select "dtime",datatype where "scale" = scale and "dtime' between starttime and endtime
# and service_id
servicestats = Servicestats.objects.raw('SELECT distinct dtime,'+ datatype + ' FROM servicestats WHERE scale = '+ scale + ' AND dtime between '+ starttime + ' and '+ endtime + 'and service_id = '+ id.id)
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment;filename="export.csv"'
writer = csv.writer(response)
for s in servicestats:
writer.writerow([s.dtime,s.datatype])
return response
else:
forms = ServicesForm
return render_to_response('monitor/monitor.html', {'forms':forms},
context_instance = RequestContext(request))
models.py :
class Servicenames(models.Model):
id = models.IntegerField(primary_key=True)
servicename = models.CharField(unique=True, max_length=255)
class Meta:
db_table = u'servicenames'
def __unicode__(self):
return self.servicename
class Servicestats(models.Model):
# service_id = models.IntegerField(primary_key=True)
service_id = models.ForeignKey(Servicenames)
dtime = models.DateTimeField(primary_key=True)
scale = models.IntegerField(primary_key=True)
cnt = models.IntegerField()
min = models.FloatField()
max = models.FloatField()
avg = models.FloatField()
threenines = models.FloatField()
class Meta:
db_table = u'servicestats'
forms.py :
class ServicesForm(forms.Form):
services=Servicenames.objects.all()
service = forms.ModelMultipleChoiceField(queryset=services,widget=forms.Select(attrs={'class':'colr',}))
scales = Servicestats.objects.values_list('scale', flat=True).distinct()
scale = forms.ModelChoiceField(queryset=scales,widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
DATATYPE_CHOICES = (
('cnt', 'cnt'),
('min', 'min'),
('max', 'max'),
('avg', 'avg'),
('threenines','threenines'),
)
datatype = forms.ChoiceField(choices = DATATYPE_CHOICES,widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
starttime = forms.DateTimeField(initial = datetime.now)
endtime = forms.DateTimeField(initial = datetime.now)
template.html :
% extends "bbase.html" %}
{% block extrascripts %}
$("#monitor").addClass("active")
{% endblock %}
{% block content %}
<div class="main">
<p>Welcome to the monitoring management system.</p>
<p>Stay tuned for more details</p>
</div>
<div>{{forms.service}}<span id="selection"><--Select services</span></div>
<div>{{forms.scale}}<span id="selection"><--Select resolution</span></div>
<div>{{forms.datatype}}<span id="selection"><--Select data type</span></div>
<div>{{forms.starttime}}<span id="selection"><--Select start time</span></div>
<div>{{forms.endtime}}<span id="selection"><--Select end time</span></div>
<input type = "submit" value = "Submit">
{% endblock %}
感谢您的帮助。
答案 0 :(得分:1)
您的表单周围似乎缺少form tag!
试
<form action='' method='post'>{% csrf_token %}
...your form html...
...inputs...
...submit...
</form>