【问题标题】:Django downloading a csv file with variable in nameDjango 下载名称为变量的 csv 文件
【发布时间】:2017-06-09 06:24:05
【问题描述】:

我正在尝试编写一些代码,以允许用户在按下下载按钮时根据搜索结果下载 .csv 文件。我想在 csv 文件的默认名称中包含日期,但无法使其正常工作。 views.py中的相关代码为:

now = datetime.datetime.now()
date = str(now.year)+'-'+str(now.month)+'-'+str(now.day)

response = HttpResponse(content_type='csv')
response['Content-Disposition'] = 'attachment; filename="results"+str(date)+".csv"'

这将返回默认文件名“results_+str(date)+_.csv”。

【问题讨论】:

    标签: python django


    【解决方案1】:

    你搞砸了你的'"

    response['Content-Disposition'] = 'attachment; filename="results'+str(date)+'.csv"'
    

    这会奏效。不过我建议这样做:

    response['Content-Disposition'] = 'attachment; filename="results{}.csv"'.format(str(date))
    

    还可以考虑使用strftime

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2021-08-25
      • 1970-01-01
      • 2012-02-07
      • 2016-12-01
      • 2011-01-28
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      相关资源
      最近更新 更多