如何在HTML中显示变量

Ric*_*92d -1 html python python-2.7

我正在使用Python制作一个Web应用程序,并且有一个我想在HTML页面上显示的变量.我怎么能这样做?将使用{% VariableName %}在HTML页面是正确的做法呢?

mha*_*wke 11

Flask 文档中对此进行了非常明确的解释,因此我建议您阅读它以获得完整的理解,但这是一个渲染模板变量的简单示例.

HTML模板文件存储在templates/index.html:

<html>
<body>
    <p>Here is my variable: {{ variable }}</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

简单的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 Code Online (Sandbox Code Playgroud)

运行此脚本并在浏览器中访问http://127.0.0.1:5000/.你应该看到variable渲染的值为12345