python - BadValueError:预计日期,得到你的'201-04-25'

时间:2018-04-15 18:12:46

标签: python google-app-engine

我想从datepicker获取日期并将其存储在我的模型中,但它会出现以下错误

  

python - BadValueError:预计日期,得到了'208-04-25'

型号:

from google.appengine.ext import ndb


class Bookings(ndb.Model):
    bookingName = ndb.StringProperty()
    bookingFrom = ndb.DateProperty()

班级方法:

bookingName = self.request.get('bookingName')
bookingFrom = self.request.get('bookingFrom')
user = users.get_current_user()
myuser_key = ndb.Key('MyUser', user.user_id())
myuser = myuser_key.get()
new_address = Bookings(bookingName=bookingName, bookingFrom=bookingFrom)
myuser.addresses.append(new_address)
myuser.put()
self.redirect('/')

HTML:

 <form action="/" method="post">
          Line 1: <input type="text" name="bookingName"/><br/>
          Line 2: <input type="date" name="bookingFrom"/><br/>
          <input type="submit" name="button" value="Add Address"/>
 </form>

1 个答案:

答案 0 :(得分:3)

如错误所示,您将字符串作为bookingFrom传递,但Bookings期望它是日期对象,因此您需要将其转换。

from datetime import datetime

...
new_address = Bookings(bookingName=bookingName, 
                       bookingFrom=datetime.strptime(bookingFrom, '%Y-%m-%d'))
...