django表单与多个文件字段

django表单与多个文件字段

本文介绍了django表单与多个文件字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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?

推荐答案

我晚了聚会,但我一直在试图弄清楚一段时间,终于有一个解。
看看这里使用的代码:

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表单与多个文件字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:08