我有一个带有必需 FileField 的 Django 表单(不是 ModelForm)。根据FileField 文档,验证 FileField 应该验证非空文件数据已绑定到表单,但我没有看到这种行为。相反,我可以在没有文件的情况下提交表单,并且表单通过了验证。预期的行为将是验证失败。
当在表单中指定文件时,事情会按预期工作。
我的表格看起来像这样:
class BucketUploadForm(forms.Form):
file = forms.FileField(required=True) # required=True is the default, but I'm being explicit
def clean(self):
upload_to = '/some/path'
upload_to += self.cleaned_data['file'].name # this is raising a KeyError
我的观点是这样的:
def bucket_upload(request):
if request.method == 'POST':
form = BucketUploadForm(request.POST, request.FILES)
if form.is_valid(): # this is raising the aforementioned KeyError when no file is submitted
do_stuff()
return HttpResponseRedirect(some_url)
else:
form = BucketUploadForm(initial=request.GET)
return render_to_response('handin/bucket_upload.html', {'form': form}, context_instance=RequestContext(request))
我的模板看起来像这样:
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<table>
{{ form }}
</table>
<input type="submit" value="Upload" />
</form>
回溯看起来像这样:
Django Version: 1.3.1
Python Version: 2.7.3
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'hgrepo',
'sshkey',
'handin',
'accounts']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/views/decorators/http.py" in inner
45. return func(request, *args, **kwargs)
File "/home/sduckwo/projects/webhandin/webhandin/handin/views.py" in bucket_upload
461. if form.is_valid():
File "/usr/lib/python2.7/dist-packages/django/forms/forms.py" in is_valid
121. return self.is_bound and not bool(self.errors)
File "/usr/lib/python2.7/dist-packages/django/forms/forms.py" in _get_errors
112. self.full_clean()
File "/usr/lib/python2.7/dist-packages/django/forms/forms.py" in full_clean
268. self._clean_form()
File "/usr/lib/python2.7/dist-packages/django/forms/forms.py" in _clean_form
296. self.cleaned_data = self.clean()
File "/home/sduckwo/projects/webhandin/webhandin/handin/forms.py" in clean
116. upload_to += self.cleaned_data['file'].name
Exception Type: KeyError at /courses/a/b/assignments/c/sduckwo/upload
Exception Value: 'file'
更新
从 BucketUploadForm 中删除 clean 方法会导致 FileField 的验证按预期失败。但是,我需要 clean 方法进行其他检查,因此不能永久删除它。
我还发现通过修改 clean 方法看起来像这样:
class BucketUploadForm(forms.Form):
file = forms.FileField(required=True) # required=True is the default, but I'm being explicit
def clean(self):
if 'file' not in self.cleaned_data:
raise ValidationError('No file or empty file given')
upload_to = '/some/path'
upload_to += self.cleaned_data['file'].name # this is raising a KeyError
然后验证按预期失败,但我收到两条错误消息:
- BucketUploadForm.clean() 提出的“没有给出文件或空文件”
- 由 FileField.clean() 提出的“此字段是必需的”,这是我最初所追求的。
这告诉我 FileField.clean() 正在引发 ValidationError,但该异常会以某种方式被忽略,除非 BucketUploadForm.clean() 不存在或也引发 ValidationError。
所以现在我可能离我的最终目标更近了一点,但仍然很困惑。