将数组传递到URL的替代方法是什么(URL太长) [英] What is the alternative of passing array to the URL?(URL too long)

查看:65
本文介绍了将数组传递到URL的替代方法是什么(URL太长)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从GET方法请求时,URL的创建方式类似于

WHile requesting from GET method, The url is created like

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

如果路径数量很多,这可能会很长.

This can be very long if there are large number of paths.

我想通过POST方法请求路径,因为在GET方法创建的url中请求行太大.是否有其他替代方法

I want to request the paths through the POST method because the request lines are too large in the url created by the GET method. Is there any alternatives for

request.GET.getlist('paths')

如果路径很长,则表明错误的请求,请求行太大.

if the paths are very long, then it shows Bad Request, Request Line too large.

对此有另一种选择:

request.POST.getlist('paths')

在模板中传递查询字符串中的数据以使URL不会很长并且可以下载所有文件的等效方法是什么?

What will be the equivalent way to pass the data in the query string in the template so that the url will not be very long and all files can be downloaded?

views.py

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

模板

<a href ="{% url 'test_download' %}?{% for path in paths %}paths={{ path|urlencode }}&{% endfor %}">Converted Files</a>

推荐答案

您可以使用表单而不是超链接来提交POST请求并在POST数据中发送路径.参见下面的示例代码

You could use a form instead of a hyperlink to submit a POST request and send your paths in POST data. See below for example code

<form method="POST" action="{% url 'test_download' %}">
    {% csrf_token %}
    {% for path in paths %}
        <input type="hidden" name="paths" value="{{ path }}"
    {% endfor %}
    <button type="submit">Converted Files</button>
</form>

这篇关于将数组传递到URL的替代方法是什么(URL太长)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆