【发布时间】:2013-11-10 21:23:34
【问题描述】:
我目前正在学习 Udacity 的 Web 开发课程,当我在浏览他们的一个示例源代码时,我为博客中的每个帖子生成一个永久链接,我遇到了一个疑问。现在这是我卡住的代码:
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)
现在,该课程的教授说 post_id 是在提交帖子后立即从 URL 传递的。这是相同的处理程序:
('/blog/([0-9]+)', PostPage)
那么在用户提交帖子的时候,post_id到底是怎么产生的呢?这些 id 是在什么基础上生成的?它们是随机的吗?还有,post_id 到底是怎么传入 PostPage 处理程序的 post() 函数的?
【问题讨论】:
标签: python database google-app-engine key gql