0

我正在尝试使用 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 

有什么建议么 ?!

4

1 回答 1

0

这不会是一个发布请求。这是一个获取请求。删除 if 方法 == post。

于 2013-07-30T10:20:01.630 回答