Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

python - What is the alternative of passing array to the URL?(URL too long)

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.

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.

There is an alternative for this:

request.POST.getlist('paths')

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

templates

<a href ="{% url 'test_download' %}?{% for path in paths %}paths={{ path|urlencode }}&{% endfor %}">Converted Files</a>
question from:https://stackoverflow.com/questions/65839418/what-is-the-alternative-of-passing-array-to-the-urlurl-too-long

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...