我试图在我创建的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()
堆栈跟踪非常有用,并指向functional.py in __mod__, line 160
。我已经检查了这行代码,但无法理解它。堆栈跟踪的其他部分是:
Traceback:
115. response = callback(request, *callback_args, **callback_kwargs)
44. if test.is_valid():
我已经探索了堆栈跟踪附带的POST数据,并没有看到任何异常,所以我不明白这个NoneType来自哪里。有一点需要注意的是"文件已上传"因为我从另一个表单继承了文件上传数据,所以有点误导,这就是为什么我只传入request.POST而不是request.FILES。
编辑:完整的堆栈跟踪:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/submit/
Django Version: 1.5.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')
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 "/<project_dir>/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/<project_dir>/views.py" in submitted_file
44. if test.is_valid():
File "/<project_dir>/local/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
126. return self.is_bound and not bool(self.errors)
File "/<project_dir>/local/lib/python2.7/site-packages/django/forms/forms.py" in _get_errors
117. self.full_clean()
File "/<project_dir>/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
274. self._post_clean()
File "/<project_dir>/local/lib/python2.7/site-packages/django/forms/models.py" in _post_clean
332. self.instance.clean_fields(exclude=exclude)
File "/<project_dir>/local/lib/python2.7/site-packages/django/db/models/base.py" in clean_fields
946. setattr(self, f.attname, f.clean(raw_value, self))
File "/<project_dir>/local/lib/python2.7/site-packages/django/db/models/fields /__init__.py" in clean
213. self.run_validators(value)
File "/<project_dir>/local/lib/python2.7/site-packages/django/db/models/fields /__init__.py" in run_validators
165. v(value)
File "/<project_dir>/local/lib/python2.7/site-packages/django/core/validators.py" in __call__
168. self.message % params,
File "/<project_dir>/local/lib/python2.7/site-packages/django/utils/functional.py" in __mod__
160. return six.text_type(self) % rhs
Exception Type: TypeError at /submit/
Exception Value: %d format: a number is required, not NoneType
edit2:忘了主要的黄色部分:
TypeError at /submit/
%d format: a number is required, not NoneType
Request Method: POST
Request URL: http://127.0.0.1:8000/submit/
Django Version: 1.5.1
Exception Type: TypeError
Exception Value:
%d format: a number is required, not NoneType
Exception Location: <project_dir>/local/lib/python2.7/site- packages/django/utils/functional.py in __mod__, line 160
Python Executable: <project_dir>/bin/python
Python Version: 2.7.3
Python Path:
['<project_dir>',
'<project_dir>/local/lib/python2.7/site-packages/setuptools- 0.6c11-py2.7.egg',
'<project_dir>/local/lib/python2.7/site-packages/pip-1.3.1- py2.7.egg',
'/home/trevor/django_projects/SC2Hackers/lib/python2.7/site-packages/setuptools-0.6c11- py2.7.egg',
'<project_dir>/lib/python2.7/site-packages/pip-1.3.1- py2.7.egg',
'<project_dir>/lib/python2.7',
'<project_dir>/lib/python2.7/plat-linux2',
'<project_dir>/lib/python2.7/lib-tk',
'<project_dir>/lib/python2.7/lib-old',
'<project_dir>/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'<project_dir>/local/lib/python2.7/site-packages',
'<project_dir>/lib/python2.7/site-packages']
Server time: Wed, 5 Jun 2013 19:23:31 -0500
答案 0 :(得分:5)
根据Django documentation,max_length是CharField的必需参数。
因此,将max_length添加到CharField应该修复它。
在将基于预先存在的Form的新Model和ModelForm添加到Django 1.4项目中的应用程序时,我遇到了同样的问题。我没有在Form中使用max_length(它没有任何问题),而且由于CharField对于Form和Model来说很常见,所以我尝试使用该字段。 KABOOM!
幸运的是,我在文档中注意到了“必需”这个词。错误消息和回溯不是很有帮助。