-1

我已经在我当前的 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 文件可提供正确的数据。

你能帮助我吗 ?

4

1 回答 1

1

尝试这个:

抱歉回复错误。教程说:

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

首先创建一个响应,然后编写内容

于 2013-12-05T10:30:59.777 回答