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 Welcome to Flask Request args

Flask Basics

Challenge 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
from flask import request

app = Flask(__name__)

def index(name="Treehouse"):
    return 'Hello {}'.format('name')
Seth Kroger
Seth Kroger
56,407 Points

I don't know Flask so I can't tell you if you have the query part down. However, format usual takes variables but the single quotes makes it a literal string. I would first remove them, i.e., format(name).

1 Answer

John Lack-Wilson
John Lack-Wilson
8,169 Points

The challenge is asking you to use Flask's request module to get the name argument from the URL query string.

I feel it may be easier to explain this and be more understandable by providing the successful code below:

from flask import Flask, request

app = Flask(__name__)

def index():
    return 'Hello ' + request.args['name']

We can simply have one @app.route() declaration here, as we are going to request additional information from the URL. This is achieved using the .args function which asks for the 'name' part of the URL, and of course this is concatenated with 'Hello'.