我有以下代码:
DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME):
return ndb.Key('Guestbook', guestbook_name)
class Author(ndb.Model):
identity = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=False)
class Greeting(ndb.Model):
author = ndb.StructuredProperty(Author)
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
"""picture = ndb.BlobProperty(indexed=False)"""
class MapPage(webapp2.RequestHandler):
def get(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greetings_query = Greeting.query(
ancestor=guestbook_key(guestbook_name)).order(-Greeting.date)
greetings = greetings_query.fetch(10)
user = users.get_current_user()
template_values = {
'user': user,
'user_mail': user.email(),
'greetings': greetings,
'guestbook_name': urllib.quote_plus(guestbook_name),
'def_name': DEFAULT_GUESTBOOK_NAME
}
path = os.path.join(os.path.dirname(__file__), 'mappage.html')
self.response.out.write(template.render(path, template_values))
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = Author(
identity=users.get_current_user().user_id(),
email=users.get_current_user().email())
greeting.content = self.request.get('content')
"""greeting.picture = self.request.get('picture')"""
greeting.put()
query_params = {'guestbook_name': guestbook_name}
self.redirect('/mappage?' + urllib.urlencode(query_params))
基本上,类Guestbook将Greeting存储到数据存储区中,MapPage会检索它。
但是,每当我在评论中添加代码时,即
picture = ndb.BlobProperty(indexed=False)
到课程问候和
greeting.content = self.request.get('picture')
到类Guestbook,代码无法运行。我已经尝试将图片更改为StringProperty但它仍然无效。