0
votes

The code's all correct but am not able to show out the desirable output on the browser- the browser alerts an internal server message(500) and the terminal shows the following error:

~/tracks/ $ flask run * Serving Flask app "application.py" (lazy loading) * Environment: development * Debug mode: off * Running on https://3405db29-f322-4a37-845f-11d0e562946b-ide.cs50.xyz:8080/ (Press CTRL+C to quit) * Restarting with stat [2020-09-13 07:29:50,889] ERROR in app: Exception on / [GET] Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functionsrule.endpoint File "/home/ubuntu/tracks/application.py", line 10, in index return render_template("index.html", number=number) File "/usr/local/lib/python3.7/site-packages/flask/templating.py", line 138, in render_template ctx.app.jinja_env.get_or_select_template(template_name_or_list), File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 930, in get_or_select_template return self.get_template(template_name_or_list, parent, globals) File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 883, in get_template return self._load_template(name, self.make_globals(globals)) File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 857, in _load_template template = self.loader.load(self, name, globals) File "/usr/local/lib/python3.7/site-packages/jinja2/loaders.py", line 117, in load source, filename, uptodate = self.get_source(environment, name) File "/usr/local/lib/python3.7/site-packages/flask/templating.py", line 60, in get_source
return self._get_source_fast(environment, template) File "/usr/local/lib/python3.7/site-packages/flask/templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: index.html
192.168.74.236 - - [13/Sep/2020 07:29:50] "GET / HTTP/1.0" 500 -

3

3 Answers

0
votes

Program is looking for a file named index.html in a directory named templates under ~/tracks directory, but it is not found.

0
votes

The traceback often provides useful information about the problem. In your terminal output, you can see "raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: index.html", then you will need to check if your view function return a template called index.html. You either need to create the template index.html under templates folder or change the return statement.

Besides, you can enable debug mode with FLASK_ENV:

$ export FLASK_ENV=development
$ flask run

Then the prettied trackback will display on the browser instead of the 500 error page.

0
votes

In addition to what @Grey Li has said, your index.html template cannot be found either because:

  • You do not have index.html file in your templates folder or
  • The path to your index.html file is incorrect.

Template nonexistent

If the file is nonexistent, you need to create it:

$ touch index.html 

Consider your current working directory to ensure that you are creating it at the right place. The example above assumes you are at the templates sub-folder in your terminal. Otherwise, you may want to add the path to templates sub-folder as:

$ touch app/templates/index.html

# wherever you might be

Incorrect Path

Simply modify your path inside render_template. Assume you you are using an application factory where each module has its own templates sub-folder. You would do:

@bp.route('/login', methods=['GET', 'POST'])
def login():
   # ...
   return render_template('auth/index.html')