0👍
I ended it up using FILEwrapper:
def send_file(request):
filename = settings.MEDIA_ROOT +'/'+ 'output1.csv'
#filename= r"C:\Users\A6B0SZZ\PycharmProjects\sample\media\output1.csv"
download_name ="output1.csv"
wrapper = FileWrapper(open(filename))
response = HttpResponse(wrapper,content_type='text/csv')
response['Content-Disposition'] = "attachment; filename=%s"%download_name
return response
2👍
For the main issue with your download not working, please take a look at this answer because the first argument for your HttpResponse should be your data to actually send.
Now, also you should do is look at interacting with the storage classes & MEDIA_ROOT
.
This will enable your project to work locally or remotely on a server. Looking at what you’ve posted I’ll assume your settings.py
contains something like MEDIA_ROOT = 'C:\users\A6B0SZZ\PycharmProjects\sample\media'
You might want to consider some more generic, reusable paths in your settings.py
(depending on how your project is structured, but this is what I have);
SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_DIR = os.path.abspath(os.path.join(SETTINGS_DIR, '../'))
BASE_DIR = os.path.abspath(os.path.join(PROJECT_DIR, '../'))
STATIC_ROOT = os.path.join(BASE_DIR, 'static-collection')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Some good reading here would be Managing Files which takes you over the various ways to create files & access their paths.
- [Answered ]-Python tests. Patch method from library in venv
- [Answered ]-Django: assert 'Many-to-many' relation exists in test
0👍
If you want to send the file to the browser that way, the file has to be un a directory directly accessible by webserver. In most cases, the folder where all Python code is stored is not accesible.
Try to put the file in a directory accesible by webserver. Another way of sending the file is reading it through Python and sending it inline, like the user is doing in this question: django return file over HttpResponse – file is not served correctly
- [Answered ]-My api call doesnot work in javascript but works fine with in postman and browser
- [Answered ]-Using ElasticSearch as Database/Storage with Django