0

I am implementing csv donwloand function referring to this page.
Even though I don't get any error message, cannot get csv file downloaded.
Does anyone know what is the problem for this implementation?
Below is the code to download csv file from database.

class timeCSVexport(View):

    def get(self,request,pk,keyword):

        key=keyword.replace("_"," ")
        queryset=timeseries.objects.filter(html__pk=pk).filter(keyword=key)

        bio = BytesIO()
        data=json.loads(list(queryset)[0].df)
        df=pd.DataFrame.from_dict(data,orient='index').T
        df.index=pd.to_datetime(df.index)
        df1=df.sort_index()

        sheet=key[:31] if len(key)>31 else key
        print (sheet)
        writer=pd.ExcelWriter(bio,engine='xlsxwriter')
        df1.to_excel(writer,sheet_name=sheet)
        writer.save()

        bio.seek(0)
        workbook=bio.getvalue()

        response = StreamingHttpResponse(workbook,content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        response['Content-Disposition'] = 'attachment; filename=%s' % pk

        return response
Katsuya Obara
  • 903
  • 3
  • 14
  • 30
  • 1
    You should use request.FILES to get your file upload then use pandas.read_csv to read the csv into a df. – Taylor Aug 01 '18 at 16:15
  • I already have data stored in database and want to download that file as csv file. I confirmed that data extraction from database and converting data structure to dataframe worked fine. – Katsuya Obara Aug 02 '18 at 00:06
  • I apologies I miss understood your question. Are you getting any errors? Also is it writing a blank file? – Taylor Aug 02 '18 at 14:29

0 Answers0