0
0
Flaskframework~20 mins

Request context in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Flask Request Context
📖 Scenario: You are building a simple Flask web app that greets users by their name passed in the URL. You will learn how Flask's request context works to access URL parameters.
🎯 Goal: Create a Flask app that reads a name parameter from the URL and returns a greeting message using Flask's request context.
📋 What You'll Learn
Create a Flask app instance
Use Flask's request object to get URL parameters
Define a route that accepts a name parameter
Return a greeting message including the name
💡 Why This Matters
🌍 Real World
Web apps often need to read user input from URLs to customize responses, like greeting users by name or filtering data.
💼 Career
Understanding request context is essential for backend web development with Flask, a popular Python web framework.
Progress0 / 4 steps
1
Set up Flask app and import request
Import Flask and request from flask. Create a Flask app instance called app.
Flask
Need a hint?

Use from flask import Flask, request and then app = Flask(__name__).

2
Define a route to accept a name parameter
Use the @app.route decorator to define a route /greet that accepts GET requests. Define a function called greet to handle this route.
Flask
Need a hint?

Use @app.route('/greet', methods=['GET']) and define def greet():.

3
Access the name parameter from the request
Inside the greet function, use request.args.get('name') to get the name parameter from the URL query string. Store it in a variable called user_name.
Flask
Need a hint?

Use user_name = request.args.get('name') to get the name from the URL.

4
Return a greeting message using the name
Return a string greeting the user by name using an f-string: f"Hello, {user_name}!". This completes the Flask route function.
Flask
Need a hint?

Use return f"Hello, {user_name}!" to send the greeting back.