File attachment path issue in CSV with django

254 views Asked by At

i have implemented csv in my current Python Django project.

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'

code is working fine. and file with name abc.csv created successfully . but download option come with wrong name .

i have created file with name : abc.csv under custom_report and custom_report folder reside in my project folder. (e.g. projectname/custom_report/abc.csv). i found file under this location. ::

my project structure are:

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

                      projetname / custom_report /abc.csv 

** my issue issue :**

file come with new name custom_data_abc.csv. with blank data. while abc.csv file under the custom_report is availabe with correct data.

can you help me ?

1

There are 1 answers

0
Vaibhav Chidgopker On BEST ANSWER

Try this:

Sorry for the wrong reply .The tutorial says that:

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

First create a response and then write the content