我试图在我创建的ModelForm上调用is_valid(),由于某种原因,它给出了异常:
TypeError at /submit/
%d format: a number is required, not NoneType
有问题的视图是这样的:
def submitted_file(request):
if request.method == 'POST':
test = PickUserForm(request.POST)
if test.is_valid():
print "test"
test.save()
return HttpResponse("File uploaded.")
与此ModelForm连接:
class PickUserForm(ModelForm):
class Meta:
model = Submission
widgets = {
'comment' : Textarea(attrs={'cols':80, 'rows':20}),
}
fields = ['p1_name', 'p2_name', 'p1_url', 'p2_url', 'p1_uid', 'p2_uid', 'p1_subregion', 'p2_subregion', 'comment', 'hacker', 'file']
提交模型非常简单:
class Submission(models.Model):
p1_name = models.CharField()
p2_name = models.CharField()
p1_url = models.CharField()
p2_url = models.CharField()
p1_uid = models.IntegerField()
p2_uid = models.IntegerField()
p1_subregion = models.IntegerField()
p2_subregion = models.IntegerField()
comment = models.CharField()
hacker = models.CharField()
file = models.CharField()
stacktrace并不是很有帮助,它指向functional.py in __mod__, line 160。我已经检查了那行代码,但没有意义。stacktrace的其他部分是:
Traceback:
115. response = callback(request, *callback_args, **callback_kwargs)
44. if test.is_valid():
我仔细研究了stacktrace附带的POST数据,没有发现任何异常,因此我不知道此NoneType的来源。需要注意的一件事是“文件上传”有点误导,因为我要从另一种形式继承文件上传数据,这就是为什么我只传递request.POST而不传递request.FILES的原因。
相关分类