我是 GAE 的新手,正在尝试我的第一个示例应用程序。我写了一个简单的 HTML 表单。单击提交时,需要将字段中的详细信息保存在数据存储中。
我的问题是我收到数据类型异常,我不知道我哪里出错了
import cgi
import datetime
import urllib
import wsgiref.handlers
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class Champion(db.Model):
champion_first_name = db.StringProperty()
champion_last_name = db.StringProperty()
champion_email = db.EmailProperty()
champion_phone_code = db.IntegerProperty()
champion_phone_number = db.IntegerProperty()
diabetic_first_name = db.StringProperty()
diabetic_last_name = db.StringProperty()
diabetic_age = db.IntegerProperty()
diabetic_gender = db.StringProperty()
diabetic_email = db.EmailProperty()
diabetic_phone_code = db.IntegerProperty()
diabetic_phone_number = db.IntegerProperty()
diabetic_city = db.StringProperty()
diabetic_zipcode = db.IntegerProperty()
diabetic_since = db.IntegerProperty()
diabetic_relationship = db.StringProperty()
checkup_date = db.DateProperty ()
md_advert_feedback = db.StringProperty()
timestamp = db.DateTimeProperty(auto_now_add=True)
class MainPage(webapp.RequestHandler):
def get(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'main.html')
self.response.out.write(template.render(path, template_values))
def post(self):
pledge_data = Champion(champion_first_name = cgi.escape(self.request.get('champ_first_name')),
champion_last_name = cgi.escape(self.request.get('champ_last_name')),
champion_email = cgi.escape(self.request.get('champ_email')),
champion_phone_code = cgi.escape(self.request.get('champ_phone_code')),
champion_phone_number = cgi.escape(self.request.get('champ_phone_number')),
diabetic_first_name = cgi.escape(self.request.get('diab_first_name')),
diabetic_last_name = cgi.escape(self.request.get('diab_last_name')),
diabetic_age = cgi.escape(self.request.get('diab_age')),
diabetic_gender = cgi.escape(self.request.get('diab_gender')),
diabetic_email = cgi.escape(self.request.get('diab_email')),
diabetic_phone_code = cgi.escape(self.request.get('diab_phone_code')),
diabetic_phone_number = cgi.escape(self.request.get('diab_phone_number')),
diabetic_city = cgi.escape(self.request.get('diab_city')),
diabetic_zipcode = cgi.escape(self.request.get('diab_zip')))
# diabetic_since = cgi.escape(int(self.request.get('diab_since'))))
# diabetic_relationship = cgi.escape(self.request.get('diab_relationship')),
# md_advert_feedback = cgi.escape(self.request.get('md_ad_feedback'))
# checkup_date = cgi.escape(self.request.get('checkup_date')),
# )
pledge_data.put()
application = webapp.WSGIApplication([
('/', MainPage),
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
我得到的错误是
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 703, in __call__
handler.post(*groups)
File "C:\Users\Rishav\Documents\Google App Engine\helloworld\main.py", line 54, in post
diabetic_zipcode = cgi.escape(self.request.get('diab_zip')))
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 945, in __init__
prop.__set__(self, value)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 599, in __set__
value = self.validate(value)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 3141, in validate
% (self.name, type(value).__name__))
BadValueError: Property champion_phone_number must be an int or long, not a unicode
我绝对肯定答案会让我大吃一惊,但我现在不明白。我相信我已经输入了正确的数据库模型。此外,我是否在 webform 表单字段中实际输入任何数据也没有什么区别。即使我将字段保留为空然后点击提交,我也会收到相同的错误。
编辑:在这里操作。以防有人对我所做的事情感兴趣;一旦我将所有必填字段数据转换为 int,我发现另一个错误。如果我的任何字段具有空值,我会收到 valueerror 错误。现在我想把我所有的数据输入验证留在客户端。所以我将代码更改为;
def post(self):
pledge_data = Champion()
try:
pledge_data.champion_first_name = cgi.escape(self.request.get('champ_first_name'))
pledge_data.champion_last_name = cgi.escape(self.request.get('champ_last_name'))
pledge_data.champion_email = cgi.escape(self.request.get('champ_email'))
pledge_data.champion_phone_code = int(cgi.escape(self.request.get('champ_phone_code')))
pledge_data.champion_phone_number = int(cgi.escape(self.request.get('champ_phone_number')))
pledge_data.diabetic_first_name = cgi.escape(self.request.get('diab_first_name'))
pledge_data.diabetic_last_name = cgi.escape(self.request.get('diab_last_name'))
pledge_data.diabetic_age = int(cgi.escape(self.request.get('diab_age')))
pledge_data.diabetic_gender = cgi.escape(self.request.get('diab_gender'))
pledge_data.diabetic_email = cgi.escape(self.request.get('diab_email'))
pledge_data.diabetic_phone_code = int(cgi.escape(self.request.get('diab_phone_code')))
pledge_data.diabetic_phone_number = int(cgi.escape(self.request.get('diab_phone_number')))
pledge_data.diabetic_city = cgi.escape(self.request.get('diab_city'))
pledge_data.diabetic_zipcode = int(cgi.escape(self.request.get('diab_zip')))
pledge_data.diabetic_since = int(cgi.escape(self.request.get('diab_since')))
# diabetic_relationship = cgi.escape(self.request.get('diab_relationship')),
# md_advert_feedback = cgi.escape(self.request.get('md_ad_feedback'))
# checkup_date = cgi.escape(self.request.get('checkup_date')),
# )
except ValueError:
pass
pledge_data.put()