close

[Solved] jinja2.exceptions.TemplateNotFound error

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error jinja2.exceptions.TemplateNotFound error in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How jinja2.exceptions.TemplateNotFound error Error Occurs?

Today I get the following error jinja2.exceptions.TemplateNotFound error in python.

How To Solve jinja2.exceptions.TemplateNotFound error Error ?

  1. How To Solve jinja2.exceptions.TemplateNotFound error Error ?

    To Solve jinja2.exceptions.TemplateNotFound error Error 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.

  2. jinja2.exceptions.TemplateNotFound error

    To Solve jinja2.exceptions.TemplateNotFound error Error 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.

Solution 1

(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

Solution 2

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read