0
0
Flaskframework~30 mins

Accessing query parameters in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing query parameters in Flask
📖 Scenario: You are building a simple web app using Flask. You want to read values that users send in the URL after the question mark, called query parameters. For example, in /search?term=python, the query parameter is term with value python.
🎯 Goal: Create a Flask app that reads a query parameter called name from the URL and shows a greeting message using that name.
📋 What You'll Learn
Create a Flask app with one route /greet
Read the query parameter name from the URL
If name is missing, use Guest as default
Return a greeting message like Hello, <name>!
💡 Why This Matters
🌍 Real World
Web apps often need to read information from the URL to customize what they show. Query parameters are a common way to send small pieces of data from the browser to the server.
💼 Career
Knowing how to access query parameters is essential for backend web developers working with Flask or similar frameworks to build interactive web applications.
Progress0 / 4 steps
1
Set up the Flask app and route
Write code to import Flask and request from flask. Create a Flask app called app. Define a route for /greet using @app.route('/greet'). Inside the route function called greet, return the string 'Hello!'.
Flask
Need a hint?

Remember to import both Flask and request from the flask package. Use @app.route('/greet') to create the route.

2
Get the query parameter name
Inside the greet function, create a variable called name. Use request.args.get('name') to get the name query parameter from the URL.
Flask
Need a hint?

Use request.args.get('name') to read the query parameter named name.

3
Set a default value for name
Change the code to set name to request.args.get('name', 'Guest') so that if the name parameter is missing, it uses 'Guest' as the default value.
Flask
Need a hint?

Use the second argument of get to provide a default value.

4
Return a greeting message using the name
Change the return statement to return a string that says Hello, <name>! using an f-string with the variable name.
Flask
Need a hint?

Use an f-string like f'Hello, {name}!' to include the variable in the string.