2

I try to open a index.html using Flask

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>

When i run the run.py file i have the error jinja2.exceptions.TemplateNotFound: index.html

The files are in the filed called app except the run.py, this filed is in the general filed

Structure directory

tortuga445
  • 35
  • 1
  • 1
  • 5
  • 1
    Because `render_template` is expecting your templates to be in a subdirectory called `templates` by default – roganjosh Dec 28 '19 at 11:03

3 Answers3

6

Okay, let me elaborate in steps.

Step 1. Create a new folder called "templates"

Step 2. Move your "index.html" in to the "templates" folder

Step 3. In your index function, return render_template('index.html', user=user)

Hope this helps, feel free to ask any questions!

0

I think the template 'index.html' needs to be within a ./templates/ folder, i.e. CODIGO/templates/index.html

Ref- https://kite.com/python/docs/flask.render_template

ADK
  • 1
  • 3
  • I do that and the error is the same, i change the routes.py `return render_template('templates/index.html', user=user)` but not is solved – tortuga445 Dec 28 '19 at 11:44
  • Yeah you are supposed to keep the index.html in the templates folder but in the render_template("index.html", user=user) – Simran Sharma May 26 '23 at 04:44
0

In the app folder, make a folder called templates. Put index. HTML in there. Then do this: return render_template('index.html', user=user)

RedBallG
  • 25
  • 1
  • 6