gpt4 book ai didi

python - Django forms.FileField 验证

转载 作者:行者123 更新时间:2023-11-28 16:49:07 27 4
gpt4 key购买 nike

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

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

关于python - Django forms.FileField 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10055029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com