python-Django Forms.FileField验证

我在表单中使用了文件字段和文本字段

class SolutionForm(forms.Form):
    text = forms.CharField(widget=forms.Textarea, required=False)
    file  = forms.FileField(required=False)

我还定义了一种清洁方法:

def clean(self,*args, **kwargs):
    if (not self.data['file'] or self.data['text']):
         raise forms.ValidationError('Please enter your code in text box or upload an arrpopriate file.')
return self.data['text']

但是提交表单时出现以下错误:

"Key 'file' not found in <QueryDict: {u'text': [u''], u'csrfmiddlewaretoken': [u'c52ea10c16620d3ebf0a20f015a3711d'], u'version': [u'C 1.1.1']}>"

如何引用文件字段?

任何帮助,将不胜感激.

谢谢,

潘卡

解决方法:

# inside SolutionForm class
def clean(self):
    if not (self.cleaned_data['file'] or self.cleaned_data['text']):
        raise forms.ValidationError('Please enter your code in text box or upload an appropriate file.')
    return self.cleaned_data
上一篇:php-FILTER_VALIDATE_EMAIL


下一篇:javascript-jQuery-填充下拉表单选项值