有人向我解释烧瓶中的渲染

问题描述 投票:-3回答:1

我是Flask的新手。我理解下一个代码,我认为将文章变量传递给模板articles.html

    @app.route('/articles')
    def articles():
    return render_template("articles.html", articles=Articles)

但是下面的一个是我不明白的:

@app.route('/articles/<string:id>/')
def article(id):
    return render_template("article.html", id=id)

有人可以解释一下吗? "<string:id>"和参数id在funcion文章中的含义以及id。提前致谢!

python flask
1个回答
0
投票

在Flask中,/articles/<string:id>/指定了一个路径参数,以便在向例如

/articles/123/

变量id的值为123 - 作为字符串,因为这是指定的类型。

因此,对于任何给定的文章名称,这将传递给渲染以与article.html一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.