考虑一个包含 FileField
的表单:
from django import forms
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
处理这个表单的视图将在 request.FILES
中接收文件数据,它是一个字典,包含表单中每个 FileField
(或 ImageField
,或其他 FileField
子类)的键。所以上述表单中的数据将以 request.FILES['file']
的形式被访问。
注意 request.FILES
只有当请求方法是 POST
,至少有一个文件字段被实际发布,并且发布请求的 <form>
有 enctype="multipart/form-data"
属性时,才会包含数据。否则 request.FILES
将为空。
大多数情况下,你需要像将上传的文件绑定到表单中 里描述的那样将文件数据从 request
传递给表单。示例如下:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import UploadFileForm
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render(request, 'upload.html', {'form': form})
注意我们必须将 request.FILES
传入到表单的 构造方法中,只有这样文件数据才能绑定到表单中。
我们通常可能像这样处理上传文件:
def handle_uploaded_file(f):
with open('some/file/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
使用 UploadedFile.chunks()
而不是 read()
是为了确保即使是大文件也不会将我们系统的内存占满。
如果想要在 FileField
上的 Model
保存文件,使用 ModelForm
会让这一过程变得简单。当调用 form.save()
时,文件对象将会被保存在对相应 FileField
的 upload_to
参数所指定的地方:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import ModelFormWithFileField
def upload_file(request):
if request.method == 'POST':
form = ModelFormWithFileField(request.POST, request.FILES)
if form.is_valid():
# file is saved
form.save()
return HttpResponseRedirect('/success/url/')
else:
form = ModelFormWithFileField()
return render(request, 'upload.html', {'form': form})
如果你准备手工构建对象,你可以指定来自 request.FILES
的文件对象到模型里的文件对象:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import UploadFileForm
from .models import ModelWithFileField
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
instance = ModelWithFileField(file_field=request.FILES['file'])
instance.save()
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render(request, 'upload.html', {'form': form})
如果您在请求之外手动构建对象,则可以将类似文件的对象分配给 FileField
:
from django.core.management.base import BaseCommand
from django.core.files.base import ContentFile
class MyCommand(BaseCommand):
def handle(self, *args, **options):
content_file = ContentFile(b'Hello world!', name='hello-world.txt')
instance = ModelWithFileField(file_field=content_file)
instance.save()
如果你想使用一个表单字段上传多个文件,则需要设置字段的 widget
的 multiple HTML
属性。
from django import forms
class FileFieldForm(forms.Form):
file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
然后覆盖 FormView
子类的 post
方法来控制多个文件上传:
from django.views.generic.edit import FormView
from .forms import FileFieldForm
class FileFieldFormView(FormView):
form_class = FileFieldForm
template_name = 'upload.html' # Replace with your template.
success_url = '...' # Replace with your URL or reverse().
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('file_field')
if form.is_valid():
for f in files:
... # Do something with each file.
return self.form_valid(form)
else:
return self.form_invalid(form)