JS:
jQuery(function($){
$(".collectionlist").click(function() {
alert('alert')
$.ajax({
data:{
csrfmiddlewaretoken: ('{{csrf_token}}'),
collection:('{{collection}}')
},
type:'POST',
url: '/collect/',
cache:false,
success: function(response) {
return true;
}
});
});
})
HTML:
<form action="." method="POST">{% csrf_token %}
<div id="report-collect">
<button type="submit" class="collectionlist" >Save-Spreadsheet <img src="{{ STATIC_URL }}images/button-icon-ir-fwd.png" width="12" height="17" alt="" /></button>
</form>
</div>
views.py
def collect(request):
if request.method == 'POST':
csv_report = request.POST.getlist('collection')
""""""""""""""""""
""""""""""""""""""
# setup CSV export
response = HttpResponse(mimetype='text/csv')
filename = "%s-%s.csv" % ('incident-reporter', datetime.datetime.now().strftime('%Y-%m-%d'))
response['Content-Disposition'] = 'attachment; filename=%s' % filename
writer = UnicodeWriter(response, encoding='utf-8')
for writerow in csv_report:
writer.writerow(writerow)
return response
view方法用于将数据导出到.csv文件中。单击保存电子表格按钮,会发出ajax请求,我将数据列表(集合)作为参数传递。我在控制台中没有收到任何错误,但如果我点击保存到电子表格按钮,我收到的消息为POST:localhost/collect/
,但不会以csv格式导出。
我无法追查问题所在,需要帮助。