Flask template not found even when file exists and templates folder is called 'templates'?

Issue

As the title says, I have problems to render a template which exists in the correct folder which is supposed to have the correct name:

flaskapp/
     app.py
     /templates
        website.html

and the usual code

 from flask import Flask, render_template
 app = Flask(__name__)

 @app.route("/")
 def main():
     return render_template('website.html')

 if __name__ == '__main__':
     app.run()`

which yields the error TemplateNotFound: website.html

As the answers of this question and this other question suggest, the problem is either the location of the html file or the name of the templates folder, however in my case I don’t have these problems.

Now it’s important to say that in fact, a few weeks ago I was able to render my template without any problem, with exactly the same app.py file, the same website.html file and the same location.

However, I created a copy of app.py, say app1.py, another route similar to flaskapp, say flaskapp1 and some other small modifications (which I don’t remember at this moment) because I wanted to do some tests in the app1.py file without modifying my original app.py but this is when the TemplateNotFounderror started to arise. Then I deleted the route flaskapp1, the file app1.py and leave everything exactly as it was before creating duplicates but I can’t get rid of the TemplateNotFound error now.

And now it’s been 3 weeks trying to solve this issue without success. So any help will be appreciated.

EDIT: I created a new folder test with a python file app1.py. Next to the python file I created the folder templateswhich contains a html file website1.html.

This is the website1.html file:

<!DOCTYPE html>
<html>
<head> Hello! </head>
<body> 
<p> How are you? </p>
</body>
</html>

This is the app1.py file:

from flask import Flask, render_template

app1=Flask(__name__)

@app1.route("/")
def main():
    return render_template("website1.html")

if __name__ == "__main__":
    app1.run()

This is the route:

    test/
     app1.py
     /templates
        website1.html

The folder test is located at C:\Users\Alcatraz\Documents

and when I run the app1.py file I get the following error:

[2017-11-04 21:04:55,176] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\flask\app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\flask\app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "<ipython-input-10-773353175e19>", line 7, in main
return render_template("website1.html")
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\flask\templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\jinja2\environment.py", line 851, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\jinja2\environment.py", line 812, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\jinja2\environment.py", line 774, in _load_template
cache_key = self.loader.get_source(self, name)[1]
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\flask\templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\Alcatraz\Anaconda2\lib\site-packages\flask\templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)
TemplateNotFound: website1.html

Solution

Since you haven’t installed your project as a package in the virtualenv, you need to run from the root directory of the project so that Python and Flask get the correct working directory. Otherwise Flask has no idea how to find the templates.

Also, you really shouldn’t run a Flask application line by line in an IPython session, you should run it with the flask command.

set FLASK_APP=app.py
flask run

Or if you really want to use app.run for some reason, you should run the file with Python.

python app.py

Answered By – davidism

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published