当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Simple Blog on Google App Engine - no entries being displayed

问题描述:

I have seen similar questions asked on the same topic, which is coding a very simple blog with Python and hosted on GAE. Apologies if I have missed the solution in one of the answers.

I can't see any database entries being displayed at all. Here is my code:

Entity:

class Comment(db.Model):

name = db.StringProperty(required=True)

comment = db.TextProperty(required=True)

created = time.strftime("%d/%m/%Y")

Main Handler:

class MainPage(Handler):

def render_front(self, name="", comment="", error=""):

comments = db.GqlQuery("SELECT * FROM Comment ORDER BY created DESC")

self.render("front.html", name=name, comment=comment, error=error, comments=comments)

def get(self):

self.render_front()

def post(self):

name = self.request.get("name")

comment = self.request.get("comment")

if name and comment:

c = Comment(name=name, comment=comment)

c.put()

time.sleep(0.5)

self.redirect("/")

So this will be displayed in the HTML:

{% for e in comments %}

<div class="comment">

<div class="comment-name">

{{e.name}}

</div>

<pre class="comment-content">

{{e.comment}}

<br>

on {{e.created}}

</pre>

</div>

{% endfor %}

The problem is that the program seems to completely ignore the above for block. I managed to make it work for a while but I checked it many times and can't see where the problem is.

Any help will be appreciated. Thanks in advance.

网友答案:

A few ways to check through this:

  • Use the admin console to look at your Datastore. Do you have records in there?

  • Are your datetime properties being stored correctly? For docs on DS and Datetime, see here: https://developers.google.com/appengine/docs/python/datastore/typesandpropertyclasses#DateTimeProperty

  • Have you made the date a 'dateTimeProperty' type? See the example here: developers.google.com/appengine/docs/python/ndb/queries See also here: stackoverflow.com/questions/9700579/gql-select-by-date

  • Try removing the ORDER BY clause in your GQL. I'm wondering if there's something funky with the date/time variable

  • Try running locally using the App Engine SDK, and use the logs in that to see the behavior. When you're happy that everything works you can upload it to GAE.

  • Don't use the templating engine -- for now just do the loop completely in Python -- and self.response.write stuff out. This will tell you if your query works properly.

Hope it works out for you! :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: