Create a link to a list of post titles with webapp2 and Jinja2 (Google App Engine)

After taking an online course on Udacity, I started developing a simple Q&A website that allows users who are logged in to post questions from a simple html input form. Once a user submits a question, it then stores the question in the Google DB data store. Most of the below codes would be a review material but if you have never seen the below code, I hope this helps.

class Post(db.Model):
    question = db.StringProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)
    last_modified = db.DateTimeProperty(auto_now = True)

When storing each post, permalink for each post is created by using the below code.

p = Post(question = question)
self.redirect('/questions/%s' % str(p.key().id())

Then I need to create a class that handles the above link. Also, remember to add a URL handler for permalink.

Handling URL for permalink. ([0-9]+) is regular expression and this will be the “post_id” for the PostPage class.

app = webapp2.WSGIApplication([('/', MainPage),
				('/questions/([0-9]+)', PostPage),
class PostPage(Handler):
    def get(self, post_id):
        key = db.Key.from_path('Post', int(post_id))
        post = db.get(key)

        if not post:

        self.render("permalink.html", post = post)

Each permalink will render “permalink.html”

Just add this code to your html. This is Jinja2

{{post.render() | safe}}

Now when users submit a question, permalink is created for each question. But how do we display a list of our questions on the front page and attach a link to each question? I was stuck with this problem and the whole point of this blog post was to explain how to link each question.

It is quite simple. Just wrap your post title with the below code.

<a href="/{{post.key().id()}}">{{post.question}}</a>

Of course, you will need a front page handler to render the questions list.


One thought on “Create a link to a list of post titles with webapp2 and Jinja2 (Google App Engine)

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 )

Facebook photo

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

Connecting to %s