【问题标题】:File attachment path issue in CSV with django使用 django 的 CSV 文件附件路径问题
【发布时间】:2013-12-05 10:17:09
【问题描述】:

我已经在我当前的 Python Django 项目中实现了 csv。

writer = csv.writer(open('custom_data/abc.csv', 'w+'))
            print "abc"
            headers = []
            for i in data_desc:

                headers.append((i[0].replace('_', ' ')).upper())
                j = j+1          

            j=1
            writer.writerow(headers)
            """
                fill data into csv cells
            """
            for value in data.fetchall():
                k=0
                no_record_check=1

                row = []
                for val in value:

                    row.append(val)

                    k = k+1       
                j=j+1
                writer.writerow(row)
        except:
            print "Exception here after printing"              

            #pass                    

        response = HttpResponse(mimetype='text/csv')
        now = datetime.datetime.now().strftime('%m-%d-%Y_%H:%M:%S')
        response['Content-Disposition'] = 'attachment; filename= custom_data/abc.csv'

代码运行良好。并成功创建了名为 abc.csv 的文件。但下载选项名称错误。

我在 custom_report 下创建了名称为 abc.csv 的文件,custom_report 文件夹位于我的项目文件夹中。 (例如项目名称/custom_report/abc.csv)。我在这个位置找到了文件。 ::

我的项目结构是:

                      projectname / app / app_name/ forms.py, views.py...

                      projetname / custom_report /abc.csv 

** 我的问题:**

文件带有新名称 custom_data_abc.csv。与空白数据。而 custom_report 下的 abc.csv 文件可以使用正确的数据。

你能帮帮我吗?

【问题讨论】:

  • 你为什么在这个问题上加上“php”标签?它与python、django和csv完全相关
  • 请清理代码!缩进代码,删除除了没有尝试之外的错误,删除注释和空行...

标签: python django csv django-errors


【解决方案1】:

试试这个:

抱歉回复错误。教程说:

response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=unruly.csv'
writer = csv.writer(response)

先创建响应,再写入内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2013-07-14
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    相关资源
    最近更新 更多