gpt4 book ai didi

python - GoogleAppEngine 数据存储未返回任何记录

转载 作者:行者123 更新时间:2023-11-30 23:55:25 25 4
gpt4 key购买 nike

我正在学习如何使用 GoogleAppEngine,并选择 Python 作为语言。

这是我的代码:

import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Greeting(db.Model):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)

class BlogPost(db.Model):
author = db.UserProperty();
body = db.StringProperty(multiline=True)
postDate = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
blogPosts = db.GqlQuery("SELECT * FROM BlogPost ORDER BY date DESC LIMIT 10")
greetings = db.GqlQuery("SELECT * FROM Greeting ORDER BY date DESC LIMIT 10")

for post in blogPosts:
if post.author:
self.response.out.write('<b>%s</b>' % post.author.nickname())
else:
self.response.out.write('<b>A guest wrote:</b>')
self.response.out.write(cgi.escape(post.body))

# Write the submission form and the footer of the page
self.response.out.write("""
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")

class Guestbook(webapp.RequestHandler):
def post(self):
post = BlogPost()

if users.get_current_user():
post.author = users.get_current_user()

post.body = self.request.get('content')
post.put()
self.redirect('/')

application = webapp.WSGIApplication(
[('/', MainPage),
('/sign', Guestbook)],
debug=True)

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()

我想添加一个 BlogPost 类只是为了自己测试一下,但似乎没有记录被保存到数据存储中。我使用 Komodo Edit 作为我的 IDE,因此无法使用断点。

有什么明显的错误吗?

谢谢!

最佳答案

好吧,首先我收到以下日志错误(可能只适合我):

dev_appserver_main.py:466] <class 'google.appengine.api.datastore_errors.InternalError'>
Are you using FloatProperty and/or GeoPtProperty? Unfortunately loading float values from the datastore file does not work with Python 2.5.0.

必须使用 -c 来摆脱它标志。

第二,为什么你还有Greeting数据库模型?你没有使用它。不妨删除它。

但真正的错误是在查询 blogPosts = db.GqlQuery("SELECT * FROM BlogPost ORDER BY date DESC LIMIT 10") 中你没有date排。看看你给它起的名字:

postDate = db.DateTimeProperty(auto_now_add=True)

将您的请求修改为:blogPosts = db.GqlQuery("SELECT * FROM BlogPost ORDER BY postDate DESC LIMIT 10")它会像魅力一样发挥作用。希望这有帮助。

关于python - GoogleAppEngine 数据存储未返回任何记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4697973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com