pythonflaskroutes

Flask not loading index from static


I just started using Flask with Python 2.7 and virtualenv after it was recommended to me on Twitter. It's currently working fine for a "Hello World!" but I can't get it to serve static files. At the moment I have this in start.py:

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

@app.route('/')
def home():
    author = "Old Mother Hubbard"
    name = "Arthur Dent"
    return render_template('index.html', author=author, name=name)

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

start.py is in the project root directory, and I have the file index.html in the /static subdirectory of the project root. However, when I run the server process and browse to 127.0.0.1 I get a 500 error with this stack trace:

[2017-02-11 19:11:49,564] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "start.py", line 8, in start
    return render_template('index.html', author=author, name=name)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/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 "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/templating.py", line 57, in get_source
    return self._get_source_fast(environment, template)
  File "/home/peter/MouseV3/venv/local/lib/python2.7/site-packages/flask/templating.py", line 85, in _get_source_fast
    raise TemplateNotFound(template)
TemplateNotFound: index.html
127.0.0.1 - - [11/Feb/2017 19:11:49] "GET / HTTP/1.1" 500 -

I have tried changing index.html to ./static/index.html but this still results in basically the same error. Why am I getting a TemplateNotFound error when the template is in the folder?

I am starting the server process using python start.py from the project root directory.


Solution

  • You are getting an error because you placed the templates in the wrong directory. Have a look at quickstart. Templates and static files are two different things. Templates are usually located in the templates subdirectory (that can be changed by setting a parameter in the App constructor).