Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Welcome to Flask View Args Through URL

Flask: Add a new route to hello() that expects a name argument. The view will need to accept a name argument, too.

Hey all. I have been having a lot of trouble with the editor and Workspace on Chrome for Win 8.

The last few live quiz I tested in my Workspace and they worked but the quiz did not accept the answer after a few tries...not sure what's going on. It's difficult to see if I am doing the code right or wrong.

Anyway here is my code, is this right?

flask_app.py
from flask import Flask

app = Flask(__name__)


@app.route('/')
@app.route('/hello/<name>')
def hello():
     return 'Hello {}'.format(name)

4 Answers

You want to use __name__ to pass in for the app object, since it refers to the namespace. Name should be the parameter for the hello() view.

def hello(name):

Thank you. This make sense, so before I was trying to call the variable that had no parameters defined..!

Bo Yuan
Bo Yuan
20,928 Points
from flask import Flask

app = Flask(__name__)


@app.route('/')
@app.route('/<name>')
def hello(name):
     return 'Hello Student {}'.format(name)
Andrew Winkler
Andrew Winkler
37,739 Points

Well, this code answers question one and sort of goes into answering question 2...

They don't want the second route at python /hello/<name> , just at python /<name>. The hello() in the problem description refers to the view name that you are adding the route to.

Also, you need to make sure to add name as a parameter to the view, as per: "The view will need to accept a name argument, too."

John,

Thanks for the info.

So what am I doing wrong with the parameter here?

from flask import Flask

app = Flask(__name__)


@app.route('/<name>')
def hello():
     return 'Hello {}'.format('name')
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Your view still doesn't take a name argument. The view is a function that has a route. So it should be hello(name), not just hello(). Also, you were supposed to add a new route that expects a name. The hello() view should have two routes by this point.

Kenneth thanks for the explanation. I started my first answer with this and was receiving errors that my first exercise was broken. When I made a new browser window and entered the same information it worked..

alex faroro
alex faroro
895 Points

from flask import Flask

app = Flask(name)

@app.route('/')

@app.route('/<name>')

def hello(name): return 'Hello Student {}'.format(name)