gpt4 book ai didi

python - 谷歌应用引擎 : Permalink generation

转载 作者:搜寻专家 更新时间:2023-10-30 20:28:17 24 4
gpt4 key购买 nike

我目前正在学习 Udacity 的 Web 开发类(class),当我学习他们的示例源代码之一时,我遇到了一个关于为博客中的每个帖子生成永久链接的示例源代码。现在这是我卡住的代码:

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)

现在,该类(class)的教授说一旦提交帖子,post_id 就会从 URL 传入。这是相同的处理程序:

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

那么用户提交帖子时post_id到底是怎么生成的呢?这些 ID 是在什么基础上生成的?它们是随机的吗?另外,post_id 实际上是如何传递到 PostPage 处理程序的 post() 函数中的?

最佳答案

论坛上的一些您可能会觉得有用的帖子 -

  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

post_id 是帖子的实际数据库 (db) id。因此,您只需要在 Posts 数据库上为请求的特定页面 ID 执行 get_by_id()。

是的,当您向其中添加帖子时,数据库处理程序会自动生成 ID。并且 id 是随机生成的,以避免聚类。 ( Corrected by Daniel )

对于传递给处理程序的变量 (blog_id),请查看第三个链接中的答案。

有关详细信息,请参阅以上链接中的帖子。此外,论坛是一个很好的聚会场所。如果您遇到困难,很可能其他人也遇到了同样的情况并且他们在论坛上提出了问题!

保持大胆!

关于python - 谷歌应用引擎 : Permalink generation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com