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

1
  • 1
    Because render_template is expecting your templates to be in a subdirectory called templates by default Commented Dec 28, 2019 at 11:03

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

2 Comments

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
Yeah you are supposed to keep the index.html in the templates folder but in the render_template("index.html", user=user)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.