如何在HTML中显示变量 [英] How to display a variable in HTML

查看:5240
本文介绍了如何在HTML中显示变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个使用Python的网络应用程序,并且有一个我想在HTML页面上显示的变量。我怎么能这样做呢?在HTML页面中使用 {%VariableName%} 是否是正确的方法? 解决方案

在Flask 文档所以我建议你阅读它以获得完整的理解,但这里是一个非常简单的渲染模板变量的例子。



存储在 templates / index.html :

 < html> 
< body>
< p>下面是我的变量:{{variable}}< / p>
< / body>
< / html>

和简单的Flask应用程序:

  from flask import Flask,render_template 

app = Flask('testapp')

@ app.route('/')
def index():
返回render_template('index.html',variable ='12345')

if __name__ =='__main__':
app.run )

运行此脚本并访问 http://127.0.0.1:5000/ 在您的浏览器中。你应该看到变量的值为 12345


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?

解决方案

This is very clearly explained in the Flask documentation so I recommend that you read it for a full understanding, but here is a very simple example of rendering template variables.

HTML template file stored in templates/index.html:

<html>
<body>
    <p>Here is my variable: {{ variable }}</p>
</body>
</html>

And the simple Flask app:

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

这篇关于如何在HTML中显示变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆