Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a django application that converts txt to csv files. Now, I want to create a view function to download the newly converted csv files.

This downloads abcd0000.csv but how to download files that have just been converted. How to use the path dynamically for the newly created csv files in the download function?Any ideas or suggestions?

What I have tried:

Python
 views.py 
def submit(request):
         # this function converts txt to csv files. For example abcd.txt is converted into abcd0000.csv and abcd0000.csv is stored in /main/temp/ folder.

def download(request):
    file_path = os.path.join(BASE_DIR, 'main/temp/abcd0000.csv')
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read()) 
            response['Content-Disposition'] = 'inline; filename=' +os.path.basename(file_path)
            return response
    raise Http404
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900