django多个图像上传商店上传的图像列表

时间:2015-03-19 17:55:21

标签: python django forms

我的表格

<form method="get" action="" enctype="multipart/form-data">{% csrf_token %}

<input type="file" name="image" multiple />
<input type="submit" value="Submit" />

</form>

此处用户可以选择并发布多张图片。

当用户一次发布多个图像时,我想将它们保存在列表中,以便我可以迭代它们。

我的观点:

def MediaAddView(request):
# Here I want the list of all uploaded images
images = request.GET.get('image',None)
print images
if request.method == "POST":
    mod = Model()
    mod.thumb = one image from the list of image by iterating.
    mod.save()
    return HttpResponseRedirect("myUrl")
else:
    return render_to_response("anything.html", context_instance=RequestContext(request))

我想要的是当用户上传多张图片时,所有图片都应该在列表中。

就像我可以迭代for image in images

一样

这时当我print images时,它只打印上传图像的最后一张图像。如何将所有图像存储到列表中?

1 个答案:

答案 0 :(得分:2)

您无法使用GET请求上传文件,只能使用POST。然后会在request.FILES中找到这些文件,因此您可以直接迭代request.FILES.getlist('image')

相关问题