2013-06-05 27 views
3

我想打電話給is_valid()在我創建的,由於某種原因它給除外的ModelForm:%d格式:一些是必需的,而不是爲NoneType 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 
+1

你能展示你的實際模型嗎?這個模型不會按照它的方式進行編譯。另外,functional.py是什麼?它是否是圖書館的一部分? – karthikr

+0

@karthikr你是什麼意思我的實際模型?這就對了。我不知道function.py是什麼,它是Django的一部分。 –

+0

你可以包含一個正常的Python格式化版本的堆棧跟蹤嗎?另外,你使用的是什麼版本的Django和Python? –

回答

5

根據Django的documentation MAX_LENGTH爲CharField必需的參數。

因此,向CharField添加max_length應該修復它。

我剛剛在向Django 1.4項目中的應用程序添加基於預先存在的表單的新Model和ModelForm時遇到同樣的問題。我沒有在Form中使用max_length(它沒有任何問題),並且由於CharField對Form和Model而言很常見,所以我嘗試使用該字段。 KABOOM!

幸運的是,我注意到文檔中的'required'這個詞。錯誤消息和回溯不是很有幫助。

+1

錯誤消息和回溯沒有任何幫助。似乎他們應該有一個更具體的錯誤,當缺少必要的參數。或者我錯過了什麼? –

相關問題