Using Dynamic URL Parameters in Flask

Tutorial 2 of 5

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.