Python 在html模板中使用flask变量

Python 在html模板中使用flask变量,python,flask,Python,Flask,当我尝试使用url参数中的变量时,文本消失,下面是app.py代码: from flask import Flask, render_template import os TEMPLATE_DIR = os.path.abspath('templates') STATIC_DIR = os.path.abspath('static') app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR)

当我尝试使用url参数中的变量时,文本消失,下面是app.py代码:

from flask import Flask, render_template
import os

TEMPLATE_DIR = os.path.abspath('templates')
STATIC_DIR = os.path.abspath('static')

app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR)

@app.route('/<string:name>')
def home(name):
     return render_template('index.html', variable=name)
从烧瓶导入烧瓶,渲染\u模板
导入操作系统
TEMPLATE_DIR=os.path.abspath('templates')
STATIC_DIR=os.path.abspath('STATIC')
app=Flask(\uuuuu name\uuuuuuu,template\u folder=template\u DIR,static\u folder=static\u DIR)
@应用程序路径(“/”)
def home(姓名):
返回呈现模板('index.html',变量=名称)
以下是我在templates文件夹中的index.html文件中所做的操作:

<body>
<h1 class="header">Hello {{ name }}</h1>
<h3>Attention! This is a testing website with learning purpouses.</h3>
</body>

你好{{name}
注意!这是一个带有学习目的的测试网站。

但是,当我运行app.py时,不会显示错误,但不会显示Hello{{name}行

尝试此操作,因为您已将名称值分配给代码中的变量:

<body>
<h1 class="header">Hello {{ variable }}</h1>
<h3>Attention! This is a testing website with learning purpouses.</h3>
</body>

你好{{variable}}
注意!这是一个带有学习目的的测试网站。

在python代码中将
variable=name
更改为
name=name
,或者在
index.html
文件中将
{{name}
更改为
{variable}
。谢谢,为什么它不是变量?
variable=name
意味着您在
index.html
文件中有一个名为
variable
的变量,并且您给了它名称的值。因此,当您将
{{variable}}
放在
index.html
文件中时,它将被替换为它的值,这里的值就是name的值,因为您说的是
variable=name
。您可以遵循以下简单的规则:始终在等号的左侧写jinja变量,在等号的右侧写python变量。