To resolve the issue, simply create a folder name it “Templates”. Then move “index. html” into this newly created folder.
Make sure you have activated your environment and have Flask installed, and then you can start building your application. The first step is to display a message that greets visitors on the index page. You'll use Flask's render_template() helper function to serve an HTML template as the response.
You must create your template files in the correct location; in the templates
subdirectory next to the python module (== the module where you create your Flask app).
The error indicates that there is no home.html
file in the templates/
directory. Make sure you created that directory in the same directory as your python module, and that you did in fact put a home.html
file in that subdirectory. If your app is a package, the templates folder should be created inside the package.
myproject/
app.py
templates/
home.html
myproject/
mypackage/
__init__.py
templates/
home.html
Alternatively, if you named your templates folder something other than templates
and don't want to rename it to the default, you can tell Flask to use that other directory.
app = Flask(__name__, template_folder='template') # still relative to module
You can ask Flask to explain how it tried to find a given template, by setting the EXPLAIN_TEMPLATE_LOADING
option to True
. For every template loaded, you'll get a report logged to the Flask app.logger
, at level INFO
.
This is what it looks like when a search is successful; in this example the foo/bar.html
template extends the base.html
template, so there are two searches:
[2019-06-15 16:03:39,197] INFO in debughelpers: Locating template "foo/bar.html":
1: trying loader of application "flaskpackagename"
class: jinja2.loaders.FileSystemLoader
encoding: 'utf-8'
followlinks: False
searchpath:
- /.../project/flaskpackagename/templates
-> found ('/.../project/flaskpackagename/templates/foo/bar.html')
[2019-06-15 16:03:39,203] INFO in debughelpers: Locating template "base.html":
1: trying loader of application "flaskpackagename"
class: jinja2.loaders.FileSystemLoader
encoding: 'utf-8'
followlinks: False
searchpath:
- /.../project/flaskpackagename/templates
-> found ('/.../project/flaskpackagename/templates/base.html')
Blueprints can register their own template directories too, but this is not a requirement if you are using blueprints to make it easier to split a larger project across logical units. The main Flask app template directory is always searched first even when using additional paths per blueprint.
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.
(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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With