/user/42?from flask import Flask app = Flask(__name__) @app.route('/user/<int:user_id>') def show_user(user_id): return f"User ID is {user_id}"
The route uses <int:user_id> to capture an integer from the URL and passes it as an argument to the function. The function returns a string with that integer included.
/profile/<username>.Using <username> captures a string by default. The other converters expect specific types like int or float.
/item/abc123 return a 404 error?from flask import Flask app = Flask(__name__) @app.route('/item/<int:item_id>') def get_item(item_id): return f"Item {item_id}"
The route uses <int:item_id>, so Flask only matches URLs where the part after /item/ is an integer. 'abc123' is not an integer, so Flask returns 404.
/order/7/item/3?from flask import Flask app = Flask(__name__) @app.route('/order/<int:order_id>/item/<int:item_id>') def order_item(order_id, item_id): return f"Order {order_id}, Item {item_id}"
The route captures two integers from the URL and passes them as arguments to the function, which returns a formatted string with those values.
@app.route('/page/about')
def about():
return "About Page"
@app.route('/page/')
def page(page_name):
return f"Page: {page_name}"
What will be the response when visiting /page/about?Flask matches routes in the order they are added. Since /page/about is an exact static route, it matches before the dynamic /page/<page_name> route. So the 'About Page' response is returned.