简体   繁体   中英

using flask variables in html templates

When I try to use a varible from a url argument the text disappears, here's the app.py code:

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)

Here's what I am doing in the index.html file in templates folder:

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

But when I run the app.py no error is displayed but it doesnt display the Hello {{ name }} line

Try this since you have assigned your name value to variable in your code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM