1

im making a website for a project but i keep on getting this error.

jinja2.exceptions.TemplateNotFound: home.html

then i get this:

File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\J\Desktop\hello.py", line 5, in home
return render_template('home.html')
File "C:\Users\J\AppData\Local\Programs\Python\Python36\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\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)

also here is my code

from flask import Flask, render_template
app = Flask(__name__)
@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)
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
vegasnoob
  • 21
  • 1
  • 1
  • 2
  • 2
    Where are your templates? If you look at the [quick start](http://flask.pocoo.org/docs/0.12/quickstart/) then it states that `Flask will look for templates in the templates folder.`. Is that where your templates are located? – Paul Rooney Nov 03 '17 at 21:06

2 Answers2

7

You need to ensure your template html files are located in a subdirectory called templates.

If they are located there it will all work.

See the flask quickstart for a simple explanation of render_template.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • all of my html files are in the templates folder and it still doesn't work. any other suggestions? – vegasnoob Nov 03 '17 at 21:43
  • If your application is structured as a package, then it expects the templates to be in a directory `templates` under the module root. Could that be the problem? – Paul Rooney Nov 03 '17 at 21:57
1

Seems like your html files are not in the same directory. Try reading the file and see if you get can open it.

with open('home.html', 'r') as html_file:
    html= html_file.read()

or try doing this

import os.path
os.path.isfile('home.html') 

Your directory structure should look like this

/home.py
/templates
    /home.html
Tejas
  • 284
  • 2
  • 8