我尝试使用 Flask 打开 index.html
run.py
from app import app
app.run(debug = True)
__init__.py
from flask import Flask
app = Flask(__name__)
from app import routes
routes.py
from flask import render_template
from app import app
@app.route('/')
@app.route('/index')
def index():
user = 'Cala'
return render_template('index.html', user=user)
index.html
<html>
<body>
{% for user in users %}
<h1>Hola {{ user }}</h1>
<h1>Bienvenido a Olivanders</h1>
{% endfor %}
</body>
</html>
当我运行 run.py 文件时出现错误jinja2.exceptions.TemplateNotFound: index.html
这些文件位于名为app除run.py外,该字段在通用字段中
好吧,我来详细说明一下步骤。
步骤 1. 创建一个名为“templates”的新文件夹
步骤 2. 将“index.html”移至“templates”文件夹
步骤 3. 在索引函数中,返回 render_template('index.html', user=user)
希望这对您有所帮助,如有任何问题,请随时提问!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)