Python 谷歌应用引擎:永久链接生成
我目前正在学习Udacity的Web开发课程,在学习他们关于为博客中的每个帖子生成永久链接的示例源代码时,我遇到了一个疑问。下面是我被卡住的代码:Python 谷歌应用引擎:永久链接生成,python,database,google-app-engine,key,gql,Python,Database,Google App Engine,Key,Gql,我目前正在学习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
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()函数中传递的?论坛上的两篇文章,您可能会发现它们很有用- post_id是post的实际数据库(db)id。因此,对于请求的特定页面id,只需在Posts db上执行get_by_id() 是的,当您向数据库处理程序添加帖子时,它会自动生成ID。并且ID是随机生成的,以避免聚类。() 对于传递给处理程序的变量(blog_id),请查看第三个链接中的答案 有关详细信息,请参阅上述链接中的帖子。此外,论坛也是一个很好的聚会场所。如果你陷入困境,很有可能其他人也处于同样的境地&他们在论坛上发问了
保持清醒 直到你说数据存储ID是连续的那一刻,你都是对的。这在GAE中已经不是真的了:它们现在或多或少是随机分配的,以避免由于集群而导致的效率低下。链接确实帮助了很多。谢谢@Ashish!大家好,我再也不能访问共享的链接了。如果可能,它们都重定向到,请共享更新的永久链接。