[英]using flask variables in html templates
當我嘗試使用 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)
這是我在模板文件夾中的 index.html 文件中所做的:
<body>
<h1 class="header">Hello {{ name }}</h1>
<h3>Attention! This is a testing website with learning purpouses.</h3>
</body>
但是當我運行 app.py 時沒有顯示錯誤,但它沒有顯示 Hello {{ name }} 行
嘗試此操作,因為您已將名稱值分配給代碼中的變量:
<body>
<h1 class="header">Hello {{ variable }}</h1>
<h3>Attention! This is a testing website with learning purpouses.</h3>
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.