【问题标题】:Google App Engine: Permalink generationGoogle App Engine:永久链接生成
【发布时间】: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


    【解决方案1】:

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

    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 db 上为请求的特定页面 id 执行 get_by_id() 。

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

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

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

    保持大胆!

    【讨论】:

    • 在您说数据存储 ID 是连续的那一刻之前,您一直是正确的。这在 GAE 中已经有一段时间了:它们现在或多或少是随机分配的,以避免由于集群导致的低效率。
    • 链接确实有很大帮助。谢谢@Ashish!
    • 大家好,我无法再访问共享的链接。如果可能,他们都重定向到discussions.udacity.com,请分享更新的永久链接。
    猜你喜欢
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    相关资源
    最近更新 更多