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 Request args

Grant Kropf
Grant Kropf
3,421 Points

Flask Challenge 1: 200 error?

I get an 200 (routing?) error when I enter this code for Flask Challenge 1. I don't see the problem..

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route('/')
def index(name):
    return "Hello {}".format(name)

[MOD: added ```python formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Sorry, my other answer was for a different challenge. This one uses the URL query string to pass arguments not the URL itself.

Task 1 of 1: Import 'request' from 'Flask'. Then update the index view to return "Hello {name}", replacing {name} with a name argument in the query string.

from flask import Flask
# Import 'request' from 'Flask'....
from flask import request

app = Flask(__name__)

@app.route("/")
def index(name="Treehouse"):
    # with a name argument in the query string.
    name = request.args.get('name', name)
    # update the index view to return "Hello {name}"
    return "Hello {}".format(name)
Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

You also need to add a route for the name. It's also a good idea to give the name parameter a default for cases when the route '/' is used without a name.

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route('/')
@app.route('/<name>')
def index(name='Student'):
    return "Hello {}".format(name)
Grant Kropf
Grant Kropf
3,421 Points

Thanks. I tried your code (this is for challenge task 1 of 1 in flask basics), and it is telling me it didn't get the expected response.

Here is my code: It should be identical to yours.

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route('/')
@app.route('/<name>')
def index(name='Student'):
    return "Hello {}".format(name)
Grant Kropf
Grant Kropf
3,421 Points

I tried this and it didn't work, do you know why it's telling me it didn't get the expected response?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

This answer is for another challenge. Please ignore.