I want to download all the single or multiple files created by the function by zipping them.
The problem is with templates. Please suggest properly to pass the GET parameters for a list of files I got an error :
FileNotFoundError at /test/
[Errno 2] No such file or directory: '['
This is the error for improperly placing the query string that is referring to the list of files.
My views are as follows:
def submit(request):
def file_conversion(input_file,output_file_pattern,chunk_size):
output_filenames = []
with open(input_file,"r+") as fin:
# ignore headers of input files
for i in range(1):
fin.__next__()
reader = csv.reader(fin, delimiter=',')
for i, chunk in enumerate(chunked(reader, chunk_size)):
output_filename = output_file_pattern.format(i)
with open(output_filename, 'w', newline='') as fout:
output_filenames.append(output_filename)
writer = csv.writer(fout, reader, delimiter='^')
writer.writerow(fed_headers)
writer.writerows(chunk)
# print("Successfully converted into", output_file)
return output_filenames
paths = file_conversion(input_file,output_file+'{01}.csv',10000)
# paths return a list of filenames that are created like output_file1.csv,output_file2.csv can be of any limit
# when i tried paths[0], it returns output_file1.csv
context = {'paths' :paths}
def test_download(request):
paths = request.GET.get('paths')
context ={'paths': paths}
response = HttpResponse(content_type='application/zip')
zip_file = zipfile.ZipFile(response, 'w')
for filename in paths:
zip_file.write(filename)
zip_file.close()
response['Content-Disposition'] = 'attachment; filename='+'converted files'
return response
templates
<p>
<a href ="{% url 'test_download' %}?paths={{ paths|urlencode }} " download>Converted Files</a> </p>
<br>
Help me to find the issue here.
the ?path is looking for file but it has found the list.
Then I tried:
def test_download(request):
paths = request.GET.getlist('paths')
context ={'paths': paths}
response = HttpResponse(content_type='application/zip')
zip_file = zipfile.ZipFile(response, 'w')
for filename in paths:
zip_file.write(filename)
zip_file.close()
response['Content-Disposition'] = 'attachment; filename='+'converted files'
return response
templates :
<p> <a href ="{% url 'test_download' %}?paths=path1 " download>Converted Files</a> </p>
<br>
It looks for the file as
FileNotFoundError at /test/
[Errno 2] No such file or directory: '/home/rikesh/Projects/FedMall/main/temp/47QSHA19D003A_UPDATE_20210113_{:01}.csv'
the file path should be :
/home/rikesh/Projects/FedMall/main/temp/47QSHA19D003A_UPDATE_20210113_0.csv


