Flask / Flask Routing and Views
Using Dynamic URL Parameters in Flask
This tutorial will guide you through the process of using dynamic URL parameters in Flask. Dynamic parameters allow us to create more flexible and interactive routes.
Section overview
5 resourcesCovers URL routing, dynamic routes, and handling different types of requests in Flask.
Using Dynamic URL Parameters in Flask
1. Introduction
In this tutorial, we will explore how to use dynamic URL parameters in Flask, a key feature that allows us to create more flexible and functional routes in our web applications. Dynamic URL parameters are placeholders that can be filled with any value to make a URL unique. This feature is particularly useful for creating user profiles, product pages, and other types of pages that require unique identifiers.
What you will learn:
- What dynamic URL parameters are and how they work in Flask
- How to create routes that use dynamic parameters
- How to use these parameters in your Flask views
Prerequisites:
- Basic knowledge of Python
- Basic understanding of Flask and how it works
- A working installation of Flask
2. Step-by-Step Guide
In Flask, dynamic URL parameters are created using angle brackets < > in the route decorator. The syntax is as follows:
@app.route('/path/<parameter>')
The parameter within the brackets can be any string that acts as a variable name for the parameter. This variable can then be used in the function that follows.
Best Practices and Tips
- Use descriptive names for your dynamic URL parameters.
- Be cautious while using integer parameters, as they can cause unexpected results if not used carefully.
3. Code Examples
Example 1: Basic Usage
Here's a simple example that demonstrates how to use dynamic URL parameters:
from flask import Flask
app = Flask(__name__)
@app.route('/user/<username>')
def show_user_profile(username):
# username is a dynamic parameter in the URL
return 'User %s' % username
In this example, the username in the URL is a dynamic parameter. When you visit /user/johndoe, the output will be User johndoe.
Example 2: Using Integer Parameters
You can also specify the type of the parameter. Here's an example with an integer parameter:
@app.route('/post/<int:post_id>')
def show_post(post_id):
# post_id is a dynamic parameter in the URL
return 'Post %d' % post_id
In this case, post_id must be an integer. If you attempt to visit /post/abc, Flask will return a 404 error.
4. Summary
In this tutorial, we learned how to use dynamic URL parameters in Flask. We covered creating routes that use these parameters and how to use them in your Flask views.
Next steps for learning
- Learn about other types of route parameters, such as float and path.
- Discover how to handle URL parameters that aren't found.
Additional resources
- Flask Documentation
5. Practice Exercises
Exercise 1: Create a Flask app with a route that displays the square of a number. The number should be a dynamic URL parameter.
Solution:
@app.route('/square/<int:num>')
def square(num):
return 'Square of %d is %d' % (num, num**2)
Exercise 2: Create a Flask app with a route that displays a user's profile. The user's name and age should be dynamic URL parameters.
Solution:
@app.route('/profile/<username>/<int:age>')
def profile(username, age):
return 'User: %s, Age: %d' % (username, age)
Tips for further practice:
- Combine static and dynamic parts in your URLs.
- Try out dynamic URL parameters with different data types.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article