How to create a blog comment system with webapp2 (on Google App Engine) – Part 2

From the previous post, I went over storing comments into db model and this post will cover how to rendered the store comments associated with questions that have those comments.

It was actually simpler than I thought it would be. See the below code.

class PostPage(Handler):
    def get(self, post_id):
        key = db.Key.from_path('Post', int(post_id), parent=question_key())
        question = db.get(key)

        params = dict(question=question)
        #retrieve all the comments and then filter it by key
        if Reply.all():
            reply = Reply.all().filter('question_post = ', key)
            params['reply'] = reply

        if self.user:
            params['current_user'] =

        if not post:

        self.render("permalink.html", **params)

What I did here is that I fetched the question entity by creating the key. Once you receive the key, use the db.get(key) function to get the entity of the question. See the below code.

key = db.Key.from_path('Post', int(post_id), parent=question_key())
question = db.get(key)

I can now simply fetched all of my comments and use the question’s key to filter comments that only have the same key as the question.

Once you assign the filtered comments to a variable, you can simply pass the variable into the render function. You will now see the comments rendered properly under the question.

Below code is the template I used for rendering comments with Jinja2.

{% for r in reply %}
    {{r.content | safe}}
{% endfor %}

As mentioned during my previous post, this is using db model and I have migrated my datastore to ndb model. I will explain the entire migration process in my next post as I had some hiccups transitioning into using the new ndb model.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s