0
0
Flaskframework~20 mins

Route with dynamic parameters in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Dynamic Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing /user/42?
Given the Flask route below, what will be the response text when a client visits /user/42?
Flask
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}"
AUser ID is 42
BUser ID is <int:user_id>
CUser ID is user_id
D404 Not Found
Attempts:
2 left
💡 Hint
Look at how the route captures the integer parameter and uses it in the function.
📝 Syntax
intermediate
1:30remaining
Which route definition correctly captures a string username?
Select the correct Flask route decorator to capture a string username from the URL path /profile/<username>.
A@app.route('/profile/<int:username>')
B@app.route('/profile/<path:username>')
C@app.route('/profile/<float:username>')
D@app.route('/profile/<username>')
Attempts:
2 left
💡 Hint
The default converter for dynamic parts is string.
🔧 Debug
advanced
2:30remaining
Why does this route cause a 404 error for /item/abc123?
Examine the Flask route below. Why does visiting /item/abc123 return a 404 error?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/item/<int:item_id>')
def get_item(item_id):
    return f"Item {item_id}"
AThe function get_item is missing a return statement.
BThe route expects an integer, but 'abc123' is not an integer, so no match is found.
CFlask routes cannot have dynamic parameters.
DThe route should use <string:item_id> instead of <int:item_id>.
Attempts:
2 left
💡 Hint
Check the type converter in the route and the URL parameter.
state_output
advanced
2:00remaining
What is the output of this route with multiple parameters?
Given the Flask route below, what is the response when visiting /order/7/item/3?
Flask
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}"
AOrder order_id, Item item_id
BOrder <int:order_id>, Item <int:item_id>
COrder 7, Item 3
D404 Not Found
Attempts:
2 left
💡 Hint
Look at how multiple dynamic parameters are captured and used.
🧠 Conceptual
expert
3:00remaining
What happens if two routes overlap with dynamic parameters?
Consider these two Flask routes:
@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?
AAbout Page
BPage: about
C404 Not Found
DRuntimeError due to route conflict
Attempts:
2 left
💡 Hint
Flask matches routes in the order they are defined and prefers exact matches over dynamic ones.