Ex50 Internal Server Error

I received " Internal Server Error" when I run the code.

Here is the code:

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route ('/')

def index ():
        greeting = "Hello World"
        return render_template ("index.html", greeting=greeting)

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

How can I resolve this issue. I tried both Windows 8 and 10, I got the same error.

Looks like typos to me. You have a spaces in a lot of places before before the parenthesis (call).

This might be how you’ve posted the code here but if not, fix that.

I.e

def index (): 
render_template (‘index.html’)

To

def index():
render_template(‘index.html’)

Finally I figured it out. It’s the folder structure issue.

My original folder structure is like this:

gothonweb/
        gothonweb/
                app.py
        templates/
        docs/
        bin/
        tests/
              test_app.py

I moved the templates folder under gothonweb as follows:

gothonweb/
        gothonweb/
                app.py
                templates/
        docs/
        bin/
        tests/
              test_app.py

Then it works perfectly.

1 Like

Interesting, I’m curious if that’s a recent change in Flask? What version of flask did you install?

Incidentally, @gpkesley the extra space between a function name and the parens isn’t a syntax error. It is inconsistent with how everyone else types code but it’ll still run. @BennettInHouston should stop adding the space mostly because other people will complain about it, not because it’s an error of any syntax.

1 Like

Hi Zed, I followed your instructions to a tee and had the same issues regarding the Internal Server Error. Followed @BennettInHouston file structure and worked perfectly.

Just letting you know my issues and what worked for me

That means it’s some code issue, so in the Terminal output you should have a more full description of the error. Can you paste it here?

@zedshaw Hope I’ve done this right!

 (lpthw) 192-168-1-102:gothonweb Michael$ python app.py
     * Serving Flask app "app" (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: off
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    [2021-02-02 08:54:42,736] ERROR in app: Exception on / [GET]
    Traceback (most recent call last):
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
        response = self.full_dispatch_request()
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
        raise value
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
        rv = self.dispatch_request()
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "app.py", line 9, in index
        return render_template("index.html", greeting=greeting)
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/templating.py", line 138, in render_template
        ctx.app.jinja_env.get_or_select_template(template_name_or_list),
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/jinja2/environment.py", line 930, in get_or_select_template
        return self.get_template(template_name_or_list, parent, globals)
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/jinja2/environment.py", line 883, in get_template
        return self._load_template(name, self.make_globals(globals))
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/jinja2/environment.py", line 857, in _load_template
        template = self.loader.load(self, name, globals)
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/jinja2/loaders.py", line 115, in load
        source, filename, uptodate = self.get_source(environment, name)
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/templating.py", line 60, in get_source
        return self._get_source_fast(environment, template)
      File "/Users/Michael/.venvs/lpthw/lib/python3.6/site-packages/flask/templating.py", line 89, in _get_source_fast
        raise TemplateNotFound(template)
    jinja2.exceptions.TemplateNotFound: index.html
    127.0.0.1 - - [02/Feb/2021 08:54:42] "GET / HTTP/1.1" 500 -

It looks like your index.html file is not available. Look at your code and see where it should be, then make sure that file is in the right template location.

Same for me.

When I go like this (as is instructed in the manual):

Schermafbeelding 2021-03-26 183752

It does not work.

But when I go like this:

Schermafbeelding 2021-03-26 183820

Runs like a charm.

Could be due to some kind of update.

I wonder if Flask changed how it loads things, or you’ve configured it to load from a different directory?

If I have done so than I am fully unaware of how I did that xD.

There is something else I have been stuck with myself for some time.

The study drills ask to read about and implement css and html5 files.

So I made these files:

But I cannot get the css implemented in the html file:

It gives the following error when I press f12:

For some reason it thinks there is nothing in the file:

Any idea what I am doing wrong? I have a feeling it has something to with the directories again, but I have been playing around with it for a while and cannot seem to figure it out.

For that you need to setup a static/ directory that flask serves, put the static .css and other files in there (images, javascript), and then make your URLs match that. You can see the docs here:

And the sample app here:

And if you google: “flask static files” or “flask css” this will come up.

Thank you very much!

A free service run by Zed A. Shaw for learncodethehardway.org.