📖 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
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
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
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
Hint
Use return f"Hello, {user_name}!" to send the greeting back.
Practice
(1/5)
1. What does the Flask request object represent during a web request?
easy
A. The server configuration settings
B. The database connection for the app
C. The template rendering engine
D. Information about the current web request like headers and form data
Solution
Step 1: Understand the role of request in Flask
The request object holds data about the current HTTP request, such as headers, form inputs, and client IP.
Step 2: Differentiate from other Flask components
Database connections, server settings, and template engines are separate from the request context and not part of request.
Final Answer:
Information about the current web request like headers and form data -> Option D
Quick Check:
Request object = current web request info [OK]
Hint: Request object holds current HTTP request info [OK]
Common Mistakes:
Confusing request with server or database objects
Thinking request exists outside a web request
Assuming request holds app config
2. Which of the following is the correct way to import the request object in a Flask app?
easy
A. from flask import request
B. from flask import Request
C. import flask.request
D. from flask.request import request
Solution
Step 1: Recall Flask import conventions
Flask provides request as a lowercase object to import directly from flask.
Step 2: Check each option's syntax
from flask import request uses correct lowercase import. from flask import Request wrongly capitalizes. import flask.request is invalid syntax. from flask.request import request tries to import from a submodule that doesn't exist.
Final Answer:
from flask import request -> Option A
Quick Check:
Correct import is lowercase 'request' [OK]
Hint: Import request lowercase from flask [OK]
Common Mistakes:
Using uppercase 'Request' instead of 'request'
Trying to import from flask.request submodule
Using incorrect import syntax
3. Given this Flask route code, what will print(request.method) output when a user visits /hello in a browser?
C. Accessing request outside request context causes error
D. Headers attribute does not exist on request
Solution
Step 1: Identify where request.method is accessed
The last line tries to print request.method outside any route or request handling function.
Step 2: Understand Flask request context rules
The request object only works inside a request context, such as inside route functions. Accessing it outside causes a runtime error.
Final Answer:
Accessing request outside request context causes error -> Option C
Quick Check:
Request only valid inside route functions [OK]
Hint: Use request only inside route functions [OK]
Common Mistakes:
Trying to use request globally
Assuming request is always available
Confusing import errors with context errors
5. You want to log the client's IP address for every request in your Flask app. Which code correctly accesses the IP using the request context inside a route?
from flask import Flask, request
app = Flask(__name__)
@app.route('/log')
def log_ip():
# Fill in the blank
client_ip = _____
print(f'Client IP: {client_ip}')
return 'Logged'
hard
A. request.client_ip
B. request.remote_addr
C. request.ip_address
D. request.headers['Client-IP']
Solution
Step 1: Recall how Flask exposes client IP
Flask provides the client IP via request.remote_addr.
Step 2: Check other options for validity
request.client_ip and request.ip_address do not exist. request.headers['Client-IP'] may not be set reliably.
Final Answer:
request.remote_addr -> Option B
Quick Check:
Client IP = request.remote_addr [OK]
Hint: Use request.remote_addr for client IP inside routes [OK]
Common Mistakes:
Using non-existent attributes like client_ip
Trying to get IP from headers directly
Accessing request outside route
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.