BadValueError:Property year必须是int或long,而不是unicode

时间:2015-08-05 18:40:02

标签: google-app-engine

我无法弄清楚为什么我会收到此错误 - > " BadValueError:Property year必须是int或long,而不是unicode"当我已经在我的出生类下面使用IntegerProperty()时。我只是希望我的页面允许用户输入生日,验证信息,然后在下面打印相同的生日,并要求它是正确的。

import os
import webapp2
import jinja2
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
                           autoescape = True)
class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
            self.response.out.write(*a, **kw)

    def render_str(self, template, **params):
            t = jinja_env.get_template(template)
            return t.render(params)

    def render(self, template, **kw):
            self.write(self.render_str(template, **kw))

    months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    def valid_month(self, month):
        months_abbvs = dict((m[:3].lower(), m) for m in self.months)
        if month:
            short_month = month[:3].lower()
            return months_abbvs.get(short_month)
    def valid_day(self, day):
        if day and day.isdigit():
            day = int(day)
            if day in range(0,32):
                return day
    def valid_year(self, year):
        if year and year.isdigit():
            year = int(year)
            if year in range(1950,2021):
                return year

class Birth(db.Model):
month = db.StringProperty(required=True)
day = db.IntegerProperty(required=True)
year = db.IntegerProperty(required=True)


class MainPage(Handler):

     def write_form(self, error="", month="", day="", year=""):
         self.render('practice.html', error=error, month=month, day=day, year=year)

     def get(self):
         self.write_form()

     def post(self):
         month = self.request.get('month')
         day = self.request.get('day')
         year = self.request.get('year')

         user_month = self.valid_month(month)
         user_day = self.valid_day(day)
         user_year = self.valid_year(year)

         if (user_month and user_day and user_year):
            a = Birth(month=month, day=day, year=year)
            a.put()
            self.redirect('/')
            birthdate = db.GqlQuery("SELECT * from Birth limit 1")
            self.render('practice.html', birthdate=birthdate)

         else:
            error="Bro, its invalid!"
            self.write_form(error, month, day, year)




app = webapp2.WSGIApplication([('/', MainPage)], 
                          debug=True)

1 个答案:

答案 0 :(得分:1)

声明中:

a = Birth(month=month, day=day, year=year)

您正在使用原始字符串,例如,year转换后的int,例如user_year 。这解释了您所看到的错误。