具有多个文件字段的 Django 表单 [英] django form with multiple file fields
问题描述
Django 中如何处理多个文件字段.例如,如果我只有一个表单字段,我会将 request.FILES['file'] 传递给处理函数.但是当有更多文件时该怎么办?
How do you handle multiple file fields in Django. For example if I had only one form field i would pass the request.FILES['file'] to a handling function. But what is the way to go when there are more files?
推荐答案
我迟到了,但我一直在努力解决这个问题,终于找到了解决方案.看看这里使用的代码:https://code.djangoproject.com/ticket/12446
I'm late to the party, but I've been trying to figure this out for a while and finally have a solution. Have a look at the code used here: https://code.djangoproject.com/ticket/12446
您可以使用 getlist 访问多部分值.如果我的 HTML 表单是:
You can access multipart values with getlist. If my HTML form was:
<form enctype="multipart/form-data" action="" method="post">
<input type="file" name="myfiles" multiple>
<input type="submit" name="upload" value="Upload">
</form>
我处理它的 Django 代码如下所示:
My django code to process it would look like:
for afile in request.FILES.getlist('myfiles'):
# do something with afile
编写一个表单域/小部件来正确处理这个问题是我的下一步.我对使用 Django 还是比较陌生,所以我正在学习.
Writing a form field/widget to handle this properly is my next step. I'm still rather new to using Django, so I'm learning as I go.
这篇关于具有多个文件字段的 Django 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!