如何在模板中请求GET参数来获取文件列表?(Django压缩文件下载问题)

3
我希望通过打包所有由函数创建的单个或多个文件来下载它们。问题出在模板上。 请建议正确地传递GET参数以获取文件列表。 我遇到了一个错误:
FileNotFoundError at /test/
[Errno 2] No such file or directory: '['

这是未正确放置查询字符串引用文件列表时出现的错误。

我的看法如下:

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

模板

<p> 
<a href ="{% url 'test_download' %}?paths={{ paths|urlencode }} " download>Converted Files</a> </p>
<br>

帮我找到问题所在。

问题: enter image description here

?path正在寻找文件,但它却发现了列表。

然后我尝试了:

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

模板:

<p> <a href ="{% url 'test_download' %}?paths=path1 " download>Converted Files</a> </p>
<br>

它会按照以下方式查找文件:

FileNotFoundError at /test/
[Errno 2] No such file or directory: '/home/rikesh/Projects/FedMall/main/temp/47QSHA19D003A_UPDATE_20210113_{:01}.csv'

enter image description here

enter image description here

the file path should be :

/home/rikesh/Projects/FedMall/main/temp/47QSHA19D003A_UPDATE_20210113_0.csv

请包含完整的堆栈跟踪。 - markwalker_
我已经编辑过了。查询集正在寻找一个文件,但它却找到了包含文件的列表。实际上,我想将它们全部压缩并下载。 - Atom Store
我也包含了另一个堆栈跟踪。 - Atom Store
1个回答

4

request.GET.get('paths')返回的是一个列表路径的字符串表示,而不是一个列表对象。返回的值将会像这样:"['/path/file1', 'path/file2']"。当你迭代这些路径时,实际上迭代的是字符串中的每个字符。这就是为什么它首先试图找到一个名为[的目录。

要传递一个文件路径列表到GET请求中,你需要将你的URL更改为这样:

<your_url>?paths=path1&paths=path2&paths=path3...

使用以下代码在您的Python代码中获取文件路径

request.GET.getlist('paths')

如果列表中的路径数量未知呢? - Atom Store
为什么需要知道路径数量?每个路径都可以在URL末尾添加&paths=<your_path>。 - Minh Vương Phan
路径列表中可能有许多文件。我需要将它们压缩并生成一个下载文件。 - Atom Store
1
尝试将您的模板更改为<a href ="{% url 'test_download' %}?{% for path in paths %}paths={{ path|urlencode }}&{% endfor %}">转换文件</a>,看看是否有效。 - Minh Vương Phan
如果路径中有大量文件,则URL会变得非常长。请参考:https://stackoverflow.com/questions/65839418/what-is-the-post-equivalent-for-requesting-get-parameters - Atom Store

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接