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/42works. - The route
/user/Joealso works. (Do we want this?) - The route
/user/returns 404 not found - The route
/userreturns 404 not found
flask --app path_params run