0

I have been following a YouTube tutorial made by Corey Schafer using Flask. I have reached the 2nd tutorial about using html templates, but that is the only place I have reached. This is the program I am running called hello.py:

from flask import Flask, render_template

app = Flask(__name__)

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

app.run()

This is the HTML file I have been using, called home.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Flask Template Example</title>
</head>
<body>
    <div>
        <p>{{ message }}</p>
    </div>
</body>
</html>

Whenever I try to run my code, I always get the error jinja2.exceptions.TemplateNotFound: template.html. I've tried to look at all possible solutions, but none have seemed to work. How could I fix this? I'm on a Windows 64-bit machine.

  • 2
    Where is your `home.html` file located? With the default configuration, it should be in a subdirectory called `templates` – costaparas Jan 30 '21 at 02:16
  • My home.html file is in the templates subdirectory. – Module_Flow Jan 30 '21 at 03:02
  • Your error message doesn't seem consistent with the code you have posted. The file seems to be called `home.html` whereas the error talked about `template.html`. – costaparas Jan 30 '21 at 03:10

1 Answers1

2

By default un Flask, the template folder is templates/. If home.html is in the same directory as app.py, you need to set template_folder.

Here is how to fix your app.py:

from flask import Flask, render_template

app = Flask(__name__, template_folder='./')

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

app.run()

To use the default template location (which is recommended), this is the file structure you would need to have:

app.py
templates
└── home.html
Gab
  • 3,404
  • 1
  • 11
  • 22
  • Hi, I tried to paste your code into my editor with the fix, but it's still not working. I'm getting the same error jinja2.exceptions.TemplateNotFound: template.html. I also checked my file structure, and it basically matches the same structure you gave. – Module_Flow Jan 30 '21 at 03:05
  • I also checked my templating.py application in the Libs, and I found that the '.globals' and '.signals' are unresolved. Could this have caused any issues? – Module_Flow Jan 30 '21 at 03:08
  • It looks like your py file mentions `template.html` but you said your file is called `home.html`, you should check if you wrote a different filename in 2 places maybe – Gab Jan 30 '21 at 03:14