Downloading a csv file in django
- by spyder
I am trying to download a CSV file using HttpResponse to make sure that the browser treats it as an attachment. I follow the instructions provided here but my browser does not prompt a "Save As" dialog. I cannot figure out what is wrong with my function. All help is appreciated.
dev savefile(request):
try:
myfile = request.GET['filename']
filepath = settings.MEDIA_ROOT + 'results/'
destpath = os.path.join(filepath, myfile)
response = HttpResponse(FileWrapper(file(destpath)), mimetype='text/csv' )
response['Content-Disposition'] = 'attachment; filename="%s"' %(myfile)
return response
except Exception, err:
errmsg = "%s"%(err)
return HttpResponse(errmsg)
Happy Pat's day!