Question

I am currently working my way through the Web Development course from Udacity, and as I was going through one of their sample source codes regarding generation of a permalink for every individual post in a blog, I encountered a doubt. Now here's the code where I am stuck:

class PostPage(BlogHandler):
    def get(self, post_id):
        key = db.Key.from_path('Post', int(post_id), parent=blog_key())      #the created key is a key of an entity of kind 'Post' with id 'post_id' having a parent of kind defined by blog_key()
        post = db.get(key)    #the get() function basically retrieves the instance(in this case, post of the blog) 
                          #that has the 'key' as its unique identifier.
        if not post:
            self.error(404)
            return

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

Now, the professor of that course says that the post_id is passed in from the URL as soon as a post if submitted. Here's the handler for the same:

('/blog/([0-9]+)', PostPage)

So how is the post_id actually generated when the user submits the post? And on what basis are those ids generated? Are they random? Also, how is the post_id actually passed in the post() function of the PostPage handler?

Was it helpful?

Solution

Couple of posts on the forums that you might find useful -

  1. http://forums.udacity.com/questions/6012751/permalinks/6013385
  2. http://forums.udacity.com/questions/6014750/a-couple-helpful-links-for-hw-3#cs253
  3. http://forums.udacity.com/questions/6014750/a-couple-helpful-links-for-hw-3/6015526

The post_id is the actual database (db) id of the post. So, you just need to do get_by_id() on the Posts db for the particular page id requested.

Yes, the ids are automatically generated by the database handler when you add a post to it. And the ids are generated at random to avoid clustering. (Corrected by Daniel)

For the variable passing to the handler (blog_id), check out the answer in the 3rd link.

For details, refer to the posts in the above links. Also, the forums are a very good place to hangout. If you get stuck, there is a great chance others were in the same situation too & they asked out on the forums!

Stay Udacious!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top