django在哪里输出csv文件



我是Django的新手,也是web编程的新手。我正在尝试将数据库中的数据导出到csv文件中。我在这个链接中尝试过"使用python csv库":https://docs.djangoproject.com/en/dev/howto/outputting-csv/

import csv
from django.http import HttpResponse
def some_view(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='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

在哪里可以找到somefilename.csv文件?我希望它在我的应用程序文件夹中。因此,tt返回HttpResponse对象。我需要对它做些什么来获取csv吗?

根据您的代码,您正在将csv写入HTTP响应,因此它将显示在用户浏览器中。该文件从不在本地创建。

客户端的浏览器知道响应应该使用somefilename.csv文件名保存,因为这是您发送的头,但同样,该文件从未在服务器上本地创建。

最新更新