2

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.

David B.
  • 371
  • 5
  • 17

1 Answers1

3

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! :)

Laurence Moroney
  • 1,263
  • 8
  • 20
  • 1
    Thanks Laurence. So there were indeed records in the Datastore. Removing the ORDER BY clause made it work. For whatever reason, when I add it to the query, nothing is displayed. If I change the created to created = db.DateProperty(auto_now_add=True) and replace the GQL by: comments = Comment.all().order('-created'), it still does not work. – David B. Jan 07 '14 at 11:39
  • Have you made the date a 'dateTimeProperty' type? See the example here: https://developers.google.com/appengine/docs/python/ndb/queries See also here: http://stackoverflow.com/questions/9700579/gql-select-by-date – Laurence Moroney Jan 07 '14 at 16:05
  • Changing it to 'DateTimeProperty' works indeed. I can now display all the comments in descending order. Thanks again (and for the useful links as well)! – David B. Jan 08 '14 at 16:23
  • Great! Please mark my answer as the answer. I'm editing it to add the dateTimeProperty stuff. – Laurence Moroney Jan 08 '14 at 16:27