简体   繁体   English

python flask render_template

[英]python flask render_template

Could someone explain why friend = friend is necessarily in this flask function? 有人可以解释为什么在这个烧瓶函数中必须有friend = friend吗?

@app.route('/')
def index():
    friends = mysql.fetch("SELECT * FROM friends")
    return render_template('index.html', friends=friends)

In your template you have a variable called friends . 在模板中,您有一个名为friends的变量。 This code says use the friends results in the template as friends . 这段代码说使用模板中的好友结果作为friends

它将好友列表传递到模板中,以便可以显示数据。

I think what you are asking is why do you have to say "friends = friends" instead of just passing "friends". 我认为您要问的是为什么您必须说“朋友=朋友”而不是仅仅通过“朋友”。 Why the superfluous "friends"? 为什么多余的“朋友”? The way to think about it is you are passing a keyword argument, which means defining a keyword and its value like so: "keyword = value". 考虑它的方法是传递一个关键字参数,这意味着定义一个关键字及其值,如下所示:“ keyword = value”。 You might find this helpful from the Python docs . 您可能会从Python文档中找到帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM