1

我尝试使用 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,这个文件在通用文件中

结构目录

4

3 回答 3

2

好的,让我分步详细说明。

第 1 步。创建一个名为“模板”的新文件夹

步骤 2. 将您的“index.html”移动到“模板”文件夹中

步骤 3. 在您的索引函数中,返回 render_template('index.html', user=user)

希望对您有所帮助,有任何问题欢迎随时提问!

于 2020-02-17T05:38:46.503 回答
0

在 app 文件夹中,创建一个名为 templates 的文件夹。放索引。HTML 在那里。然后这样做: return render_template('index.html', user=user)

于 2019-12-28T16:50:53.417 回答
0

我认为模板'index.html'需要在一个./templates/文件夹中,即CODIGO/templates/index.html

参考https://kite.com/python/docs/flask.render_template

于 2019-12-28T11:05:18.587 回答