Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Flask Path or route parameters

In addition to having routes for fixed pathes, Flask can also handle routes where one or more parts of the path can have any value.

It can be especially useful if the response is then looked up in some sort of a database.

from flask import Flask
app = Flask(__name__)

@app.get("/")
def main_page():
    return '''
Main<br>
<a href="/user/23">23</a><br>
<a href="/user/42">42</a><br>
<a href="/user/Joe">Joe</a><br>
'''

@app.get("/user/<uid>")
def user_by_id(uid):
    return uid
  • The route /user/42 works.
  • The route /user/Joe also works. (Do we want this?)
  • The route /user/ returns 404 not found
  • The route /user returns 404 not found
flask --app path_params run