《博客文章的固定链接》章节:有这样一段代码:(详情见本书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的呀