如何通过 Flask 将 python 变量传递给我的模板?
How can I pass a python variable to my template via flask?
我用 Flask 制作了一个 Web 应用程序。我想将一个可变值传递给我的模板,以设置 属性.
在python中:
web_size="100%"
@app.route('/')
def home():
return render_template("web.html")
def size():
return "50px"
在 html5 文件中,我使用了:
width: {{url_for('size')}};
我做错了什么?以及如何使用 orl_for 命令?我想我什至不明白它是如何工作的。
您还没有将数据传递到 html sheet。例如:
message = 'Hello!'
return render_template('index.html', value= message)
要将 Python 变量传递给 Flask 模板,请执行以下操作:
在视图中首先声明 Python 变量。然后,将变量作为 render_template 方法中的参数传递给模板:
@app.route('/', methods={'GET'})
def home():
title = "Frankenstein"
return render_template('index.html', book_title=title )
然后,要在 index.html 模板中显示变量,请使用大括号:
<h3>The book named "{{book_title}}" </h3>
我用 Flask 制作了一个 Web 应用程序。我想将一个可变值传递给我的模板,以设置 属性.
在python中:
web_size="100%"
@app.route('/')
def home():
return render_template("web.html")
def size():
return "50px"
在 html5 文件中,我使用了:
width: {{url_for('size')}};
我做错了什么?以及如何使用 orl_for 命令?我想我什至不明白它是如何工作的。
您还没有将数据传递到 html sheet。例如:
message = 'Hello!'
return render_template('index.html', value= message)
要将 Python 变量传递给 Flask 模板,请执行以下操作:
在视图中首先声明 Python 变量。然后,将变量作为 render_template 方法中的参数传递给模板:
@app.route('/', methods={'GET'})
def home():
title = "Frankenstein"
return render_template('index.html', book_title=title )
然后,要在 index.html 模板中显示变量,请使用大括号:
<h3>The book named "{{book_title}}" </h3>