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:
- Route Definition: The
'/greet/<name>'defines a route that Flask will recognize. The<name>part in the URL is the path parameter. - Route Function: The
def greet(name):part defines a Python function that takes one argument (name) with the same name from the path. - Return Statement: Finally, the function returns a JSON object that includes the value of the
nameparameter.
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:
Handling Routes with and without Parameters
