Solve – Flask raises TemplateNotFound error even though template file exists

Flask raises TemplateNotFound error even though template file exists is an error which occurs when there is no home.html file in your templates directory.

In this article I am going to replicate this error and try to solve it, I am going to share the process with you and explain it step by step, I am also going to present other solutions which solved the issue for other developers.

Describing : Flask raises TemplateNotFound error even though template file exists

Let us replicate this error and create the following directory.

                                                                       #
/myproject
    app.py
    home.html
                                                                       #

Let us try to render the home.html file using the code bellow.

                                                                       #
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')
                                                                       #

As a result we get the following error, it appears Flask can not find our html template.

                                                                       #
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')
                                                                       #

There are a lot ways to solve this error, let us go step by step and find the solution which suits your case.

Solution 1 : correctly use the default directory in Flask called Templates

First of all, you should know that the default directory in Flask is the directory: Templates.

The directory should contain two html files, about.html and home.html

This is how the structure of the project should look like.

                                                                       #
project/
    hello.py        
    template/
         home.html
         about.html    
    static/
           js/
             main.js
           css/
               main.css
                                                                       #

Let us say that hello.py will look like this

                                                                       #
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)
                                                                       #

This solution should be more than enough to solve your issue.

If you find this solution helpful, please consider donating to our Kofi account in order to help us help more developers solve their errors.

Solution 2 : put the flask ‘templates’ directory one level above

Another solution is to put the templates directory one level above. Like in the example bellow :

                                                                       #
project/
    app/
        hello.py
        static/
            main.css
    templates/
        home.html
    venv/
                                                                       #

This is just a trick that worked and could be combined with other solutions, I hope this was helpful.

Solution 3 : turn on debugging with app.debug = True

Another trick is to turn on debugging which can help us find out what is exactly happening, you can do that with the line bellow

                                                                       #
app.debug = True
                                                                       #

Also make sure that the template file has the correct name, and make sure that the template file is inside the templates directory.

auth/login.html should be bellow the auth directory in the templates directory.

Summing-up

I hope my article was helpful in solving this error, errors are part of our journey of coding with Python, keep learning, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/