学习狗书flask web,博客文章的固定链接章节,为什么post(id) 函数不会陷入死循环呢?

《博客文章的固定链接》章节:有这样一段代码:(详情见本书github ‘11g’ 分支

@main.route('/post/<int:id>')
def post(id):
    post = Post.query.get_or_404(id)
    return render_template('post.html', posts=[post])

之前碰到的路由带参数的视图函数,参数要么是在html中从current.user等上下文传过来,要么是从别的函数生成后通过html传过来。而这里,上面函数路由的id 来自 post.html,而post.html 中的id 是哪儿来的,对,就是上面函数的render_template(‘post.html’, posts=[post])中posts参数传过去的。

post.html: (其实是_post.html,但是post.html导入了整个_post.html,等价的)

		 <div class="post-footer">
                <a href="{{ url_for('.post', id=post.id) }}">
                    <span class="label label-default">Permalink</span>
                </a>
            </div>

id 传过来传过去,没有一条语句能决定到底id等于多少,这不就陷入了死循环吗?
😃 稍微思考了一下,我猜我可能想错了,路由的id 来自 post.html 没错,而post.html 中的id可能是来自index函数或者user函数传过来的posts,而不是来自自家post函数。

我在github上问了书作者,回我了再来更新
更新:
在这里插入图片描述
感觉我后面思考的没错,id 来自 request URL,点击URL以后,将id传给post.html,html中通过“id=post.id”传给视图函数post的路由,就确定了是哪篇文章。
而request URL是怎么来的?就是之前在index视图函数和user视图函数里面确定是哪些博客,然后为每个博客生成的URL的呀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值