Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask Basics Templates and Static Files Simple Template

Ollie Webster
Ollie Webster
5,035 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
5,035 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.