How to save and download a csv file in django

 

import pandas as pd
 
def Category1_csv(request):

category1=Database.objects.all()
file=[]

for i in category1:
p=i.abc
row=[p,...]
file.append(row)
    #to save a file
document = pd.DataFrame(file)
document.to_csv('media/file_downloads/category1.csv')
#return HttpResponse("file createrd")
    # to download a file
data = open(os.path.join(settings.BASE_DIR,'media/file_downloads/category1.csv'),'r').read()
resp =HttpResponse(data)
resp['Content-Disposition'] = 'attachment;filename=csv file.csv'
return resp


 

Comments

Popular posts from this blog

Exporting HTML Page into PDF In Django