Join my Laravel for REST API's course on Udemy 👀

Multiple Flask routes to same function

October 8, 2020  ‐ 1 min read

In some cases you can reuse a Flask route function for multiple URLs. Or you want the same page/response available via multiple URLs. In that case you can add a second route to the function by stacking a second route decorator to the function.

The code below shows how you use the a function for multiple URLs. In case you're using parameters in one of your routes make sure to provide a default value of the function argument.

def chopsticks_view(chopstick_id=None):
    if chopstick_id:
        return "Chopstick {}".format(chopstick_id)
    return "Chopstick list"

Flask Version

$ flask --version
Python 3.8.5
Flask 1.1.2
Werkzeug 1.0.1