【发布时间】:2012-12-06 18:13:44
【问题描述】:
我正在关注 Django 文档 here 创建一个 CSV 文件。在我的 urls.py 中有url(r'^reports/csv_list_report/$', 'csv_list_report')。
我希望用户在单击下载按钮时下载 CSV 文件。所以我在这里使用jQuery:
$('#download_button').click(function(){
$.get('/reports/csv_list_report/');
});
我可以在firebug中看到响应,但是浏览器没有下载文件。
这是我的看法:
def csv_list_report(request):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename="reports.csv"'
writer = csv.writer(response)
writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])
writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"])
return response
所以也许在 GET 之后我需要写一些东西来处理成功的响应?搜索了一下,有很多关于如何创建 CSV 文件的答案,但没有找到任何答案,包括处理响应需要做什么。
【问题讨论】:
-
如果你只是想下载文件,为什么要尝试使用 jQuery?
-
它是通过点击事件触发的。我希望用户在单击下载按钮时下载文件。我将编辑我的问题。