使用数据存储区以简单GAE形式的数据类型异常

时间:2012-04-04 16:53:15

标签: python forms google-app-engine google-cloud-datastore

我是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

我绝对肯定答案会让我成为faceplam,但我现在还没有得到它。我相信我已经放入了正确的数据库模型。 另外,如果我实际在webform表单字段中输入任何数据,则没有任何区别。即使我将字段保留为null然后点击提交,我也会得到相同的错误。

编辑: OP在这里。 如果有人对我的所作所为感兴趣; 一旦我将所有必需的字段数据转换为int,我发现了另一个错误。 如果我的任何字段都有null值,我会得到valueerror eception。 现在我想在客户端留下所有数据输入验证。 所以我改变了我的代码;

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()

2 个答案:

答案 0 :(得分:2)

快速解决方法是明确施放,例如:

...
champion_phone_number = int(cgi.escape(self.request.get('champ_phone_number'))),
...

答案 1 :(得分:1)

所有请求参数都是字符串。如果你需要属性是另一种类型,如champion_phone_number你需要显式转换为int()