【发布时间】:2021-01-22 04:42:59
【问题描述】:
当从 GET 方法请求时,url 的创建方式类似于
<your_url>?paths=path1&paths=path2&paths=path3...
如果有大量路径,这可能会很长。
我想通过 POST 方法请求路径,因为 GET 方法创建的 url 中的请求行太大。有没有替代品
request.GET.getlist('paths')
如果路径很长,则显示 Bad Request, Request Line too large。
有一个替代方案:
request.POST.getlist('paths')
在模板中传递查询字符串中的数据以使url不会很长并且可以下载所有文件的等效方法是什么?
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>
【问题讨论】:
-
我觉得你可以设计得更好,在 get 请求中包含 1 个路径而不是多个。理想情况下,这应该可以解决问题。在前端,您可以遍历路径并对每个路径进行 GET 调用。
-
那么下载链接会有所不同,我已经从这些路径中压缩了文件。
标签: python django django-views django-templates request