Welcome to one more lesson! This time we will explore how to create dynamic routes using path parameters. By the end of this lesson, you'll be able to define routes with variables and return customized responses based on those variables. This is an important step in building flexible and interactive web applications and will be foundational as we continue to develop more complex APIs.
Path parameters let us make our web addresses flexible. Think of them like blank spaces in a URL that we can fill in with specific values later on.
For example, if we have a route like /greet/<name>
, the <name>
part is like a placeholder. When someone goes to /greet/John
, the <name>
gets replaced with John
. So, the name
parameter will have the value John
.
This is helpful for creating web applications that need to change based on what the user types in the URL.
Now, let's define a simple dynamic route using a path parameter. The following code example shows how to do it:
Python1@app.route('/greet/<name>', methods=['GET']) 2def greet(name): 3 return jsonify(message=f"Greetings, {name}! Welcome to the dynamic route.")
'/greet/<name>'
defines a route that Flask will recognize. The <name>
part in the URL is the path parameter.def greet(name):
part defines a Python function that takes one argument (name
) with the same name from the path.name
parameter.The function parameter name
must match the name used in the route <name>
. This allows Flask to correctly bind the value from the URL to the parameter in the function.
When you navigate to /greet/John
, for example, Flask captures John
as the name
parameter and passes it to the greet
function, returning a response like this:
JSON1{ 2 "message": "Greetings, John! Welcome to the dynamic route." 3}
Flask can handle routes that have parameters and ones that don't, even if they share a common base. For example, you can define both a route without a parameter and a route with a parameter like this:
Python1@app.route('/greet', methods=['GET']) 2def greet_default(): 3 return jsonify(message="Greetings! Welcome to the default greeting.") 4 5@app.route('/greet/<name>', methods=['GET']) 6def greet(name): 7 return jsonify(message=f"Greetings, {name}! Welcome to the dynamic route.")
This way, when someone navigates to /greet
, Flask will call the greet_default
function and return the default greeting message. When someone navigates to /greet/<name>
, Flask will call the greet
function, substituting <name>
with the specific value provided in the URL.
Flask distinguishes between these routes based on the structure of the URL, ensuring that the correct function is called.
You can also define routes with multiple path parameters. For example, if you want to create a route that captures both a user's first and last name, you can do it like this:
Python1@app.route('/greet/<first_name>/<last_name>', methods=['GET']) 2def greet_full_name(first_name, last_name): 3 return jsonify(message=f"Greetings, {first_name} {last_name}!")
For instance, navigating to /greet/John/Smith
would capture John
as first_name
and Smith
as last_name
, resulting in a response like this:
JSON1{ 2 "message": "Greetings, John Smith!" 3}
Dynamic routes with path parameters are highly useful in web applications. Here are some common use cases:
/user/<user_id>
./products/<category>
or /products/<category>/<product_id>
.These examples demonstrate how dynamic routes make your web applications more flexible and user-friendly.
In this lesson, you learned about using path parameters to create dynamic routes in Flask. We covered:
Next, you'll get hands-on practice with interactive exercises to reinforce what you've learned. As always, practicing these concepts will help solidify your knowledge. The upcoming lessons will build on these fundamentals to create more advanced APIs.