0

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
antonioACR1
  • 1,303
  • 2
  • 15
  • 28
  • 1
    What are the file permissions and ownership on `website.html`? – John Gordon Nov 05 '17 at 02:38
  • @John Gordon I'm not sure what do you mean by "file permission and ownership" but I created the python file myself in my personal computer, so I guess it is "administrator permissions" or something like that – antonioACR1 Nov 05 '17 at 03:32
  • @user322778 I meant that maybe the file _exists_, but has permissions such that the web server process can't _read_ it, and so it assumes the file does not exist. Does the flask web application run as your user id, or does it run as some other user? – John Gordon Nov 05 '17 at 03:41
  • @John Gordon I guess it runs as my user id. There's no other user as far as I know... – antonioACR1 Nov 05 '17 at 03:49
  • Why does the stack trace say `File ""` and not `File ...\app1.py`? If it's looking for the `templates` folder in the same directory as the app1 file, what directory _is_ that? – John Gordon Nov 05 '17 at 03:53
  • @John Gordon I think that's the point, I don't know why it doesn't say `File...\app1.py`. The directory is `C:\Users\Alcatraz\Documents\test\app1.py` and the directory for `templates` folder is `C:\Users\Alcatraz\Documents\test\templates`, i.e. next to the `app1.py` file. Right now my other folder `Flaskapp` is located at `C:\Users\Alcatraz\Documents\Flaskapp`. I believe Flask is getting confused because of this, but when I deleted any of them the error remains there – antonioACR1 Nov 05 '17 at 04:02

2 Answers2

1

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
davidism
  • 121,510
  • 29
  • 395
  • 339
1

Just for the sake of somebody who comes by for a soloution to the stated problem, I had an identical problem with template not beein found altough everything seemed to be configured and set up properly. No spell errors at all.

John Gorden's "permission and ownership" hint brought me then to the soloution of my problem. I configured the user and group parameter in WSGIDaemonProcess wrong. They were both set to www-data, the apache's user id by the way, but had to be set to the templet file's owner and group.

Just for whom silently wonders where WSGIDaemonProcess is used: WSGIDaemonProcess has to be added to the servers .conf file under etc/apache2/sites-available.

Maybe helpful for somebody.

Thank you John Gorden by the way!

DanCeg
  • 31
  • 4