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

发布于 2024-10-12 06:56:17 字数 2297 浏览 4 评论 0 原文

我正在学习如何使用 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,因此无法使用断点。

有什么明显的错误吗?

谢谢你!

I'm learning how to use the GoogleAppEngine with Python as the language of choice.

Here's my code:

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

I wanted to add a BlogPost class just to test things out for myself, and it seems no record is being saved to the data store. I'm using Komodo Edit as my IDE so I can't use a breakpoint.

Any glaring mistakes?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玻璃人 2024-10-19 06:56:17

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

dev_appserver_main.py:466]
您使用 FloatProperty 和/或 GeoPtProperty 吗?不幸的是,从数据存储文件加载浮点值不适用于 Python 2.5.0。

必须使用 -c 标志删除它。

其次,为什么你还有 Greeting dbModel?你没有使用它。不妨将其删除。

但真正的错误是在查询 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" ) 它会像魅力一样发挥作用。希望这有帮助。

Well, first of all I'm getting error the following log error (it maybe is just for me):

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.

Had to get rid of it using the -c flag.

Second, why do you still have the Greeting dbModel? You're not using it. Might as well just remove it.

But the real mistake is in the query blogPosts = db.GqlQuery("SELECT * FROM BlogPost ORDER BY date DESC LIMIT 10") you have no date row. Look what you named it:

postDate = db.DateTimeProperty(auto_now_add=True)

Modify your request to say: blogPosts = db.GqlQuery("SELECT * FROM BlogPost ORDER BY postDate DESC LIMIT 10") and it will work like a charm. Hope this helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文