开发者

GoogleAppEngine data store isn't returning any records

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开发者_如何学Python = 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜