Flask Raises Templatenotfound Error Even Though Template File Exists

Flask raises TemplateNotFound error even though template file exists

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.

Flask raises TemplateNotFound even though templates/ is in the Same Directory

Keep the app.py file and templates folder in the same directory. Also you can explicitly mention the templates folder name.

In your app.py;

app = Flask(__name__, template_folder='templates')

python - jinja2.exceptions.TemplateNotFound error raised when extending a template

One workaround to getting it to recognise both templates would be setting your template_folder to the base directory (in your case, 'project'):

content = Blueprint("content", __name__, template_folder="../../project")

You could then go into the desired folders from there and get your template(s).

Rendering a template would look something like this:

return render_template("content/templates/content/selection.html")

And including the base template should work with this:

{% extends "general/templates/general/base.html" %}

The reason this works as it does, is because setting template_folder applies to all the templates in that blueprint. So if you render a template from that blueprint, this template_folder will also apply to that template.

TemplateNotFound even though I can print out the directory

set EXPLAIN_TEMPLATE_LOADING option in your app and past the output here.

myApp = Flask(__name__)
myApp.config["EXPLAIN_TEMPLATE_LOADING"] = True

check this too

jinja2.exceptions.TemplateNotFound: index.html error

so you must define the template folder path like this.

if you named your template folder: Tamplates

Then use this

app = Flask(__name__, template_folder='Tamplates')

Flask can't find templates even though the file exists

It's a typo wherever you render_template:

[2021-05-26 16:41:57,986] INFO in debughelpers: Locating template 'index.hmtl':

Should be looking for index.html according to your directory structure, not index.hmtl!

Flask cannot find template file - jinja2.exceptions.TemplateNotFound

The error message says:

jinja2.exceptions.TemplateNotFound: template\profile.html

The templates folder should be named templates (in plural) and saved in the root path of the application. That is how Flask works by default.



Related Topics



Leave a reply



Submit