4

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

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']}>"

如何引用文件字段?

任何帮助,将不胜感激。

谢谢,

潘卡伊。

4

1 回答 1

2
# 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
于 2012-04-07T16:09:23.293 回答