Python 如何在 HTML 中显示变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31965558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a variable in HTML
提问by Ricky92d
I am making a web app using Python and have a variable that I want to display on an HTML page. How can I go about doing so? Would using {% VariableName %}
in the HTML page be the right approach to this?
我正在使用 Python 制作一个网络应用程序,并且有一个我想在 HTML 页面上显示的变量。我该怎么做?将使用{% VariableName %}
在HTML页面是正确的做法呢?
采纳答案by mhawke
This is very clearly explained in the Flask documentationso I recommend that you read it for a full understanding, but here is a very simple example of rendering template variables.
这在 Flask文档中有非常清楚的解释,所以我建议你阅读它以充分理解,但这里有一个非常简单的渲染模板变量的例子。
HTML template file stored in templates/index.html
:
HTML 模板文件存储在templates/index.html
:
<html>
<body>
<p>Here is my variable: {{ variable }}</p>
</body>
</html>
And the simple Flask app:
以及简单的 Flask 应用程序:
from flask import Flask, render_template
app = Flask('testapp')
@app.route('/')
def index():
return render_template('index.html', variable='12345')
if __name__ == '__main__':
app.run()
Run this script and visit http://127.0.0.1:5000/in your browser. You should see the value of variable
rendered as 12345
运行此脚本并在浏览器中访问http://127.0.0.1:5000/。你应该看到variable
渲染的值12345