Forums

500 Error, template not found!

So my Flask app returns a 500 error. In the logs, it looks like this:

     raise TemplateNotFound(template)
TemplateNotFound: index.html

The most common error that I've come across for things like this is the name of the templates folder (which is templates), and my code works on my own computer when I run it locally, although I haven't gotten it to work in a virtual environment on my own computer due to my inability to get matplotlib running.

Any help on this would be great -- PythonAnywhere seems like a great solution for running simple Flask apps, but I can't for the life of me get this to work, I've wasted hours, and I'm guessing it's something stupid that I'm doing wrong.

Thanks!

To find your templates (unless you've configured it differently), Flask uses the root_path and template_folder properties of your application object (the one you create with code like app = Flask(__name__) or similar) . Check that those are correct for the file system layout that you have on PythonAnywhere.

Thanks for getting back to me!

I have a folder called density_graph_webapp that contains density_graph.py and a folder named templates which contains my index.html.

The full error is here:

2015-06-24 20:36:56,986 :Exception on / [GET]
Traceback (most recent call last):
  File "/home/cwang912/.local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/cwang912/.local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/cwang912/.local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/cwang912/.local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/cwang912/.local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/cwang912/density_graph_webapp/density_graph.py", line 73, in index
    return render_template('index.html', form=form1, name=name1)
  File "/home/cwang912/.local/lib/python2.7/site-packages/flask/templating.py", line 127, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/home/cwang912/.local/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/home/cwang912/.local/lib/python2.7/site-packages/jinja2/environment.py", line 791, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/cwang912/.local/lib/python2.7/site-packages/jinja2/environment.py", line 765, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/cwang912/.local/lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/cwang912/.local/lib/python2.7/site-packages/flask/templating.py", line 64, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: index.html

I thought the system layout matched what the recommendations are, and I don't believe I've edited the root_path or template_folder properties, so I have no idea what's causing the problem. Is there a straightforward way to interpreting this error that I'm missing somehow?

Not sure if it matters, but when I output the app.root_path variable via the web functionality, I get /home/cwang912, instead of what I think it should be, which is /home/cwang912/density_graph_webapp. I don't know how to change this, and if I run it by command line (via python density_graph.py), I do get an output of /home/cwang912/density_graph_webapp.

Can you just set the property explicitly?

app.root_path = os.path.dirname(os.path.abspath(__file__))