Forums

Flask app - templateNotFound

My flask app is getting this error: jinja2.exceptions.TemplateNotFound: index.html
And my code looks like this:

@app.route('/')
   def index():
   """ 首页 """
   title = '欢迎来到我的学习博客:D'
   return render_template('index.html',
                          the_title = title,
                          )

The whole error looks like this:

Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
  response = self.full_dispatch_request()
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
  rv = self.handle_user_exception(e)
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
  reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
  raise value
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
  rv = self.dispatch_request()
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
  return self.view_functions[rule.endpoint](**req.view_args)
File "/home/bannngo/mysite/app.py", line 81, in index
  return render_template('index.html',
File "/usr/lib/python3.8/site-packages/flask/templating.py", line 138, in render_template
  ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/lib/python3.8/site-packages/jinja2/environment.py", line 869, in get_or_select_template
  return self.get_template(template_name_or_list, parent, globals)
File "/usr/lib/python3.8/site-packages/jinja2/environment.py", line 830, in get_template
  return self._load_template(name, self.make_globals(globals))
File "/usr/lib/python3.8/site-packages/jinja2/environment.py", line 804, in _load_template
  template = self.loader.load(self, name, globals)
File "/usr/lib/python3.8/site-packages/jinja2/loaders.py", line 113, in load
  source, filename, uptodate = self.get_source(environment, name)
File "/usr/lib/python3.8/site-packages/flask/templating.py", line 60, in get_source
  return self._get_source_fast(environment, template)
File "/usr/lib/python3.8/site-packages/flask/templating.py", line 89, in _get_source_fast
  raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.html

As a startup I really don't know how to fix this problem, please help me:(

Make sure that you have a file called index.html in the directory where you have configured Flask to look for templates (it's usually a directory called 'templates' in your web app directory).

Thanks @glenn ...but is there a way to find out where Flask is looking for templates ?

To confess, I kept it in the folder named template which is in the same place as my flask code. Illustrating:

flask_app.py
|
`-- templates
|   |
|   `-- web_page.html
|
`-- static
    |
    `-- styles.css

So, here's a snapshot of flask_app.py:

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

@app.route('/')
def start() -> str:
    return 'Start here...'

@app.route('/entry', methods=['POST', 'GET'])
def entry():
    return render_template('web_page.html')

...and here's my web_page.html:

<html>
  <head>
    <link rel="stylesheet" href="../static/styles.css" />
  </head>

  <body>
    ...
  </body>
</html>

Have you tried to use absolute paths?

Well, for templates I have not used any path at all, and rely on the templates folder in the listed working directory of my PythonAnywhere account... which is /home/banstala

For the static folder, I used relative path in the web_page.html file:

<link rel="stylesheet" href="../static/styles.css" />

So, should it simply be href="static/styles.css" instead (as the static folder resides in the working directory itself) ?

By absolute path I meant the path starting with /home/banstala/ so the full path to the static file from your home directory.

Yes, I also tried using absolute paths... both in flask_app.py:

def entry():
    return render_template('/home/banstala/templates/web_page.html')

and in web_page.html:

<link rel="stylesheet" href="/home/banstala/static/styles.css" />

But still getting an error like this:

jinja2.exceptions.TemplateNotFound: /home/banstala/templates/web_page.html

Any further clues would be highly appreciated.

Sorry for asking -- but are you sure that there is web_page.html file in /home/banstala/templates?

Well, I have actually changed those names in this post (for privacy reasons-- though names are not much indicative, I know... but still it reflects the topic I'm working on-- which is a frontier research area).

Anyway, I just now double-checked the parity of filenames by copy-pasting the (html & css) path+filenames from the referring (py & html) files on the address bar following https://www.pythonanywhere.com/user/banstala/files

The names and absolute paths are OK.

Sure. I don't think there is much more we can do without taking a look into your files. We can do that, but we want to ask for permission first. If you agree to that, we could try to debug the issue and if we find anything we will contact with you via email, so everything stays confidential.

Oh ! That's a lovely idea !!

I'll be fascinated to have that help from you. Please try that... I do grant you permission.

I trust your confidentiality... and appreciate your expertise in the matter.

render_template does not take a file path. It takes a template name. Then Flask looks for that template in the template search path. Make sure that you've set the template search directory correctly (i.e. with an absolute path) using the template_folder argument when you create your Flask object: https://flask.palletsprojects.com/en/1.1.x/api/#application-object

Just to clarify: right now, you have your templates directory inside /home/banstala, and your flask_app.py inside /home/banstala/something-private. Your templates directory should be inside /home/banstala/something-private -- that's actually the structure you described in your earlier post, but it's not how it's currently laid out. Or, alternatively, you could use the template_path Flask config option to point at /home/banstala/.

Thanks @glenn @giles

Make sure that you've set the template search directory correctly (i.e. with an absolute path) using the template_folder argument when you create your Flask object: https://flask.palletsprojects.com/en/1.1.x/api/#application-object

you could use the template_path Flask config option to point at /home/banstala/...

Actually both of these worked !

So, along with marking my problem as SOLVED, let me put the answer more clearly-- to short-circuit the external link:

  1. Put in specific absolute paths in template_folder and static_folder parameters of flask_app.py
  2. Simply used file names (rather than path+filename) in--
    • render_template function of flask_app.py
    • href value for stylesheetin web_page.html

The paths in appropriate parameters:

app = Flask(__name__,
            template_folder='/home/banstala/templates/',
            static_folder='/home/banstala/static/')

and just file names in the python file:

def entry():
    return render_template('web_page.html')

as well as in the html file:

<link rel="stylesheet" href="styles.css" />

Hi @banstala -- thanks for confirming and summing that up!