Python Flask Basics Templates and Static Files Simple Template

Ollie Webster
Ollie Webster
3,946 Points

Cannot render template.

I have tried combinations of adding/removing the @app.route('/'), and also adding and removing return` in front ofrender_template```

The Code Challenge still says that it didn't get the template.

flask_app.py
from flask import Flask
from flask import render_template

app = Flask(__name__)


@app.route('/hello/<name>')
@app.route('/')
def hello(name="Treehouse"):
    return 'Hello {}'.format(name)
    return render_template("hello.html")
templates/hello.html
<!doctype html>
<html>
<head><title>Hello!</title></head>
<body>
<h1>Howdy!</h1>
</body>
</html>

1 Answer

You only need to return render_template("hello.html"). You are returning 'Hello {}'.format(name) before it, and when you return anything from a function, the function terminates. Thus you only return 'Hello {}'.format(name), and not the template.

Ollie Webster
Ollie Webster
3,946 Points

Thanks! I just worked this out. To me this seems as an example of the Challenge trying to break up an overall goal into steps that are too small and therefore difficult to put into context of what is trying to be achieved. For example, if they just removed this second part of the challenge and skipped to the third part where it wanted you to pass name to hello.html and deal with this in hello(), I'm sure I would have automatically removed the offending code.