Forums

How to request GET parameters in templates for a list of files?

Please suggest to properly 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):
    paths = [list of filenames]
    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>

You're trying to use the string representation of a Python list in the query string, but that is not encoded properly to be transferred as part of a query string. You will need to encode the list in some way and then decode it in the test_download view so that you can turn it back into a Python list. See https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode