Using Path Parameters in Dynamic Routes

Using Path Parameters in Dynamic Routes

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.

Understanding Path Parameters

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.

Defining a Route with a Path Parameter

Now, let's define a simple dynamic route using a path parameter. The following code example shows how to do it:

@app.route('/greet/<name>', methods=['GET'])
def greet(name):
    return jsonify(message=f"Greetings, {name}! Welcome to the dynamic route.")
  1. Route Definition: The '/greet/<name>' defines a route that Flask will recognize. The <name> part in the URL is the path parameter.
  2. Route Function: The def greet(name): part defines a Python function that takes one argument (name) with the same name from the path.
  3. Return Statement: Finally, the function returns a JSON object that includes the value of the 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.

Accessing the Dynamic Route

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:

{
    "message": "Greetings, John! Welcome to the dynamic route."
}

Handling Routes with and without Parameters

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal