我正在使用 Python 制作一个 Web 应用程序,并且有一个想要在 HTML 页面上显示的变量。我该如何去做呢?会使用{% VariableName %}
在 HTML 页面中是正确的方法吗?
这在 Flask 中解释得非常清楚文档 http://flask.pocoo.org/docs/0.10/quickstart/#rendering-templates所以我建议您阅读它以充分理解,但这里有一个渲染模板变量的非常简单的示例。
HTML 模板文件存储在templates/index.html
:
<html>
<body>
<p>Here is my variable: {{ variable }}</p>
</body>
</html>
以及简单的 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()
运行此脚本并访问http://127.0.0.1:5000/ http://127.0.0.1:5000/在您的浏览器中。你应该看到的价值variable
呈现为12345
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)