3

我试图在我创建的 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
4

1 回答 1

5

根据 Django文档, max_length 是 CharField 的必需参数。

因此,将 max_length 添加到 CharField 应该可以修复它。

在 Django 1.4 项目中的应用程序中添加基于预先存在的表单的新模型和模型表单时,我遇到了同样的问题。我没有在 Form 中使用 max_length (没有任何问题),并且由于 CharField 对 Form 和 Model 很常见,所以我尝试按原样使用该字段。轰隆隆!

幸运的是,我注意到文档中的“必需”一词。错误消息和回溯不是很有帮助。

于 2013-08-25T18:34:59.007 回答