I am trying to render the file home.html
. The file exists in my project, but I keep getting jinja2.exceptions.TemplateNotFound: home.html
when I try to render it. Why can't Flask find my template?
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
/myproject
app.py
home.html
If you run your code from an installed package, make sure template files are present in directory <python root>/lib/site-packages/your-package/templates
.
Some details:
In my case I was trying to run examples of project flask_simple_ui and jinja
would always say
jinja2.exceptions.TemplateNotFound: form.html
The trick was that sample program would import installed package flask_simple_ui
. And ninja
being used from inside that package is using as root directory for lookup the package path, in my case ...python/lib/site-packages/flask_simple_ui
, instead of os.getcwd()
as one would expect.
To my bad luck, setup.py
has a bug and doesn't copy any html files, including the missing form.html
. Once I fixed setup.py
, the problem with TemplateNotFound vanished.
I hope it helps someone.
Check that:
templates
render_template
is relative to the template directory (index.html
would be directly in the templates directory, auth/login.html
would be under the auth directory in the templates directory.)If that doesn't work, turn on debugging (app.debug = True
) which might help figure out what's wrong.
(Please note that the above accepted Answer provided for file/project structure is absolutely correct.)
Also..
In addition to properly setting up the project file structure, we have to tell flask to look in the appropriate level of the directory hierarchy.
for example..
app = Flask(__name__, template_folder='../templates')
app = Flask(__name__, template_folder='../templates', static_folder='../static')
Starting with ../
moves one directory backwards and starts there.
Starting with ../../
moves two directories backwards and starts there (and so on...).
Hope this helps
When render_template() function is used it tries to search for template in the folder called templates and it throws error jinja2.exceptions.TemplateNotFound when :
To solve the problem :
I don't know why, but I had to use the following folder structure instead. I put "templates" one level up.
project/
app/
hello.py
static/
main.css
templates/
home.html
venv/
This probably indicates a misconfiguration elsewhere, but I couldn't figure out what that was and this worked.
I had the same error turns out the only thing i did wrong was to name my 'templates' folder,'template' without 's'. After changing that it worked fine,dont know why its a thing but it is.
Another explanation I've figured out for myself
When you create the Flask application, the folder where templates
is looked for is the folder of the application according to name you've provided to Flask constructor:
app = Flask(__name__)
The __name__
here is the name of the module where application is running. So the appropriate folder will become the root one for folders search.
projects/
yourproject/
app/
templates/
So if you provide instead some random name the root folder for the search will be current folder.
Another alternative is to set the root_path
which fixes the problem both for templates and static folders.
root_path = Path(sys.executable).parent if getattr(sys, 'frozen', False) else Path(__file__).parent
app = Flask(__name__.split('.')[0], root_path=root_path)
If you render templates directly via Jinja2
, then you write:
ENV = jinja2.Environment(loader=jinja2.FileSystemLoader(str(root_path / 'templates')))
template = ENV.get_template(your_template_name)
My problem was that the file I was referencing from inside my home.html
was a .j2
instead of a .html
, and when I changed it back jinja could read it.
Stupid error but it might help someone.
You need to put all you .html
files in the template folder next to your python module. And if there are any images that you are using in your html files then you need put all your files in the folder named static
In the following Structure
project/
hello.py
static/
image.jpg
style.css
templates/
homepage.html
virtual/
filename.json
I think Flask uses the directory templates
by default. So your code should be like this
suppose this is your hello.py
from flask import Flask,render_template
app=Flask(__name__,template_folder='template')
@app.route("/")
def home():
return render_template('home.html')
@app.route("/about/")
def about():
return render_template('about.html')
if __name__=="__main__":
app.run(debug=True)
And you work space structure like
project/
hello.py
template/
home.html
about.html
static/
js/
main.js
css/
main.css
also you have create two html files with name of home.html
and about.html
and put those files in templates
folder.
Source: Stackoverflow.com