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?
你如何处理Django中的多个文件字段。例如,如果我只有一个表单字段,我会将request.FILES ['file']传递给处理函数。但是当有更多文件时,该怎么办?
3 个解决方案
#1
30
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
我迟到了,但我一直试图弄清楚这一点,最后有一个解决方案。看看这里使用的代码:https://code.djangoproject.com/ticket/12446
You can access multipart values with getlist. If my HTML form was:
您可以使用getlist访问多部分值。如果我的HTML表单是:
<form enctype="multipart/form-data" action="" method="post">
<input type="file" name="myfiles" multiple>
<input type="submit" name="upload" value="Upload">
</form>
My django code to process it would look like:
我的django代码处理它看起来像:
for afile in request.FILES.getlist('myfiles'):
# do something with afile
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的新手,所以我正在学习。
#2
9
request.FILES.get('filename', None)
responds to the existence of a form-named field like this:
request.FILES.get('filename',None)响应存在一个表单命名的字段,如下所示:
<input type="file" name="filename"></input>
If you had two such fields:
如果您有两个这样的字段:
<input type="file" name="file1"></input>
<input type="file" name="file2"></input>
Then request.FILES.get('file1', None)
and request.FILES.get('file2', None)
should give you those files respectively.
然后request.FILES.get('file1',None)和request.FILES.get('file2',None)应该分别给你那些文件。
The reason for this is multipart mime. The three parts (form data, file1, file2) should be uploaded and Django's UploadFileHandler splits this apart into request.POST
and request.FILES
respectively.
原因是多部分哑剧。应该上传这三个部分(表单数据,文件1,文件2),Django的UploadFileHandler分别将它拆分为request.POST和request.FILES。
#3
0
Here's is a good link for this answer: https://github.com/Chive/django-multiupload. However, since I was not using ModelForm I had to make few changes. In view I have written below code and saved files to disk:
这是这个答案的一个很好的链接:https://github.com/Chive/django-multiupload。但是,由于我没有使用ModelForm,我不得不做一些改动。在视图中,我已将下面的代码和保存的文件写入磁盘:
for each in form.cleaned_data['attachments']:
handle_uploaded_file(each)
def uploadMyWork(request):
from .forms import UploadFileForm, handle_uploaded_file
print 'in uploadMyWork'
if request.method == 'GET':
print 'in uploadMyWork : GET'
form = UploadFileForm()
else:
form = UploadFileForm(request.POST, request.FILES)
print 'in uploadMyWork : POST'
#for each in form.cleaned_data['attachments']:
# handle_uploaded_file(each)
#return render(request, 'stitchme/uploadMyWork.html', {'msg': "file uploaded successfully"})
if form.is_valid():
print 'inside form valid'
for each in form.cleaned_data['attachments']:
handle_uploaded_file(each)
return render(request, 'stitchme/uploadMyWork.html', {'msg': "file uploaded successfully"})
print 'returning to uploadmywork'
return render(request, 'stitchme/uploadMyWork.html', {'form': form, 'msg':'hi'})
#1
30
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
我迟到了,但我一直试图弄清楚这一点,最后有一个解决方案。看看这里使用的代码:https://code.djangoproject.com/ticket/12446
You can access multipart values with getlist. If my HTML form was:
您可以使用getlist访问多部分值。如果我的HTML表单是:
<form enctype="multipart/form-data" action="" method="post">
<input type="file" name="myfiles" multiple>
<input type="submit" name="upload" value="Upload">
</form>
My django code to process it would look like:
我的django代码处理它看起来像:
for afile in request.FILES.getlist('myfiles'):
# do something with afile
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的新手,所以我正在学习。
#2
9
request.FILES.get('filename', None)
responds to the existence of a form-named field like this:
request.FILES.get('filename',None)响应存在一个表单命名的字段,如下所示:
<input type="file" name="filename"></input>
If you had two such fields:
如果您有两个这样的字段:
<input type="file" name="file1"></input>
<input type="file" name="file2"></input>
Then request.FILES.get('file1', None)
and request.FILES.get('file2', None)
should give you those files respectively.
然后request.FILES.get('file1',None)和request.FILES.get('file2',None)应该分别给你那些文件。
The reason for this is multipart mime. The three parts (form data, file1, file2) should be uploaded and Django's UploadFileHandler splits this apart into request.POST
and request.FILES
respectively.
原因是多部分哑剧。应该上传这三个部分(表单数据,文件1,文件2),Django的UploadFileHandler分别将它拆分为request.POST和request.FILES。
#3
0
Here's is a good link for this answer: https://github.com/Chive/django-multiupload. However, since I was not using ModelForm I had to make few changes. In view I have written below code and saved files to disk:
这是这个答案的一个很好的链接:https://github.com/Chive/django-multiupload。但是,由于我没有使用ModelForm,我不得不做一些改动。在视图中,我已将下面的代码和保存的文件写入磁盘:
for each in form.cleaned_data['attachments']:
handle_uploaded_file(each)
def uploadMyWork(request):
from .forms import UploadFileForm, handle_uploaded_file
print 'in uploadMyWork'
if request.method == 'GET':
print 'in uploadMyWork : GET'
form = UploadFileForm()
else:
form = UploadFileForm(request.POST, request.FILES)
print 'in uploadMyWork : POST'
#for each in form.cleaned_data['attachments']:
# handle_uploaded_file(each)
#return render(request, 'stitchme/uploadMyWork.html', {'msg': "file uploaded successfully"})
if form.is_valid():
print 'inside form valid'
for each in form.cleaned_data['attachments']:
handle_uploaded_file(each)
return render(request, 'stitchme/uploadMyWork.html', {'msg': "file uploaded successfully"})
print 'returning to uploadmywork'
return render(request, 'stitchme/uploadMyWork.html', {'form': form, 'msg':'hi'})