django - 如何将图片数组提交到视图
我从昨天开始就被这个问题困住了。我有一个表单,用户可以选择多个图片文件并提交。但是现在我在views.py里无法获取到这些POSTED
的图片文件。
我的HTML代码是:
<input type="file" name="images[]" />
<input type="file" name="images[]" />
在我的views.py里,我这样做了:
images = request.POST.getlist('images[]')
但是当我print images
时,得到的是[]
,一个空数组。我到底哪里出错了?我想获取所有选中的图片文件,并在我的视图中将它们保存到数据库里。
编辑:
这是我的HTML:
<form action="/save/" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="submit" value="send" />
</form>
这是我的views.py:
fotos = request.POST.getlist('images[]')
for i in range(len(fotos)):
print fotos[i]
这里打印出来的是[]
,而不是每个图片的名字。这个数组为什么是空的呢?
谢谢大家的帮助。
1 个回答
3
当Django处理文件上传时,这和普通的POST字段有点不同。上传的数据会放在 request.FILES
里,你需要从这里去获取这些数据。关于处理文件上传的文档是一个很好的入门资料。