django写csv文件

时间:2013-07-30 10:16:50

标签: django csv

我正在尝试使用django获取ajax数据并输出csv文件,但是,教程代码可以工作:

response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=somefilename.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 

但我的代码可以运行!

if request.method == 'POST':
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=somefilename.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 

有什么建议吗?!

1 个答案:

答案 0 :(得分:0)

这不是邮寄请求。这是一个获取请求。删除if方法== post。