Python %d格式:需要一个数字,而不是NoneType for is_valid()

Python %d格式:需要一个数字,而不是NoneType for is_valid(),python,django,Python,Django,我试图在我创建的模型表单上调用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():

我试图在我创建的模型表单上调用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.")
与此模型表单连接的:

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没有多大帮助,它指向第160行中的
functional.py。我已经查过那行代码了,但没有意义。stacktrace的其他部分包括:

Traceback:
115. response = callback(request, *callback_args, **callback_kwargs)
44. if test.is_valid():
我浏览了stacktrace附带的POST数据,没有发现任何异常,所以我不明白这个非类型是从哪里来的。需要注意的一点是,“文件上传”有点误导,因为我从另一个表单中携带了文件上传数据,这就是为什么我只传入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
环境:
申请方式:邮寄
请求URL:http://127.0.0.1:8000/submit/
Django版本:1.5.1
Python版本:2.7.3
已安装的应用程序:
(“django.contrib.auth”,
“django.contrib.contenttypes”,
“django.contrib.sessions”,
“django.contrib.sites”,
“django.contrib.messages”,
“django.contrib.staticfiles”,
'django.contrib.admin')
已安装的中间件:
('django.middleware.common.CommonMiddleware',
“django.contrib.sessions.middleware.SessionMiddleware”,
“django.middleware.csrf.CsrfViewMiddleware”,
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
回溯:
get_响应中的文件“//local/lib/python2.7/site packages/django/core/handlers/base.py”
115响应=回调(请求,*回调参数,**回调参数)
提交文件中的文件“//views.py”
44如果test.u有效():
中的“//local/lib/python2.7/site packages/django/forms/forms.py”文件有效
126返回self.is_bound而非bool(self.errors)
文件“//local/lib/python2.7/site packages/django/forms/forms.py”中出现错误
117self.full_clean()
文件“//local/lib/python2.7/site packages/django/forms/forms.py”的完整格式
274自我清洁
文件“//local/lib/python2.7/site packages/django/forms/models.py”位于
332self.instance.clean_字段(exclude=exclude)
文件“//local/lib/python2.7/site packages/django/db/models/base.py”位于clean_字段中
946setattr(self,f.attname,f.clean(原始值,self))
文件“//local/lib/python2.7/site-packages/django/db/models/fields/__init___;.py”在clean中
213self.run_验证器(值)
运行验证程序中的文件“//local/lib/python2.7/site-packages/django/db/models/fields/_init__.py”
165v(价值)
调用中的文件“//local/lib/python2.7/site packages/django/core/validators.py”__
168self.message%params,
文件“//local/lib/python2.7/site packages/django/utils/functional.py”位于__
160返回六个。文本类型(自身)%rhs
异常类型:TypeError at/submit/
异常值:%d格式:需要数字,而不是非非类型
编辑2:忘记了主要的黄色部分:

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
TypeError at/submit/
%d格式:数字是必需的,不是非类型
申请方式:邮寄
请求URL:http://127.0.0.1:8000/submit/
Django版本:1.5.1
异常类型:TypeError
异常值:
%d格式:数字是必需的,不是非类型
异常位置:/local/lib/python2.7/site-packages/django/utils/functional.py,在第160行的
Python可执行文件:/bin/Python
Python版本:2.7.3
Python路径:
['',
“/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg”,
“/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”,
“/lib/python2.7/site packages/pip-1.3.1-py2.7.egg”,
“/lib/python2.7”,
“/lib/python2.7/plat-linux2”,
“/lib/python2.7/lib-tk”,
“/lib/python2.7/lib old”,
“/lib/python2.7/lib dynload”,
“/usr/lib/python2.7”,
“/usr/lib/python2.7/plat-linux2”,
“/usr/lib/python2.7/lib-tk”,
“/local/lib/python2.7/site-packages”,
“/lib/python2.7/site包”]
服务器时间:2013年6月5日星期三19:23:31-0500
根据Django max_的说法,CharField的一个必需参数是max_length

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

在Django 1.4项目中,我在向应用程序添加新模型和基于先前存在的表单的ModelForm时遇到了同样的问题。我没有在表单中使用max_length(这没有任何问题),因为CharField在表单和模型中很常见,所以我尝试按原样使用该字段。卡布姆


幸运的是,我注意到文档中有“required”一词。错误消息和回溯没有多大帮助。

您能显示您的实际型号吗?这个模型不会按现在的方式编译。还有,什么是functional.py?它是图书馆的一部分吗?@karthikr你说我的模型是什么意思?就是这样。我不知道functional.py是什么,它是Django的一部分。您能提供一个普通Python格式的堆栈跟踪版本吗?另外,您使用的是什么版本的Django和Python?@DavidK.Hess我刚刚编辑了这篇文章。它似乎在试图为您的模型的属性生成验证失败消息时爆炸了。似乎某个地方向您的模型添加了一个验证器,但其limit_值为None。您确定此处显示的提交模型确实是代码使用的模型吗?错误消息和回溯根本没有帮助。似乎当缺少必需的参数时,它们应该有一个更具体的错误。还是我遗漏了什么?