【问题标题】:jinja2.exceptions.TemplateNotFound: index.htmljinja2.exceptions.TemplateNotFound:index.html
【发布时间】:2023-03-22 02:07:01
【问题描述】:

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

【问题讨论】:

  • 因为render_template 期望您的模板默认位于名为templates 的子目录中

标签: python html flask browser server


【解决方案1】:

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

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

第 2 步。将“index.html”移动到“模板”文件夹中

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

希望对您有所帮助,如有任何问题,请随时提出!

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我认为模板“index.html”需要位于./templates/ 文件夹中,即CODIGO/templates/index.html

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

      【讨论】:

      • 我这样做了,错误是一样的,我改变了 routes.py return render_template('templates/index.html', user=user) 但没有解决
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 2020-03-20
      • 2020-12-30
      相关资源
      最近更新 更多