0
0
Flaskframework~30 mins

Route with dynamic parameters in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Flask Route with Dynamic Parameters
📖 Scenario: You are building a simple web app using Flask. You want to create a route that can accept a dynamic username in the URL and greet that user personally.
🎯 Goal: Build a Flask app with a route that uses a dynamic parameter called username in the URL path. When a user visits /hello/<username>, the app should greet them by name.
📋 What You'll Learn
Create a Flask app instance named app
Define a route with the URL pattern /hello/<username>
Create a view function named hello_user that accepts username as a parameter
Return a greeting string that includes the username
💡 Why This Matters
🌍 Real World
Web apps often need to handle user-specific pages or data by capturing parts of the URL dynamically.
💼 Career
Understanding dynamic routes is essential for backend web development roles using Flask or similar frameworks.
Progress0 / 4 steps
1
Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Need a hint?

Use Flask(__name__) to create the app instance.

2
Define the route with dynamic parameter
Use the @app.route decorator to define a route with the URL pattern /hello/<username>.
Flask
Need a hint?

Use angle brackets <> around username in the route URL.

3
Create the view function to accept the parameter
Define a function named hello_user that takes username as a parameter and returns a greeting string including the username.
Flask
Need a hint?

Use an f-string to include username in the returned greeting.

4
Add the app run command
Add the code to run the Flask app with app.run() inside the if __name__ == '__main__': block.
Flask
Need a hint?

This block ensures the app runs only when the script is executed directly.