0
0
Flaskframework~20 mins

URL building with url_for in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Builder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What URL does url_for generate for a simple route?
Given this Flask route, what URL will url_for('hello') produce?

from flask import Flask, url_for
app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hello!'
A"/hello/world"
B"/hello/"
C"/"
D"/hello"
Attempts:
2 left
💡 Hint
Think about the exact route path defined in the decorator.
state_output
intermediate
1:30remaining
What URL is generated with dynamic route parameters?
Consider this Flask route with a dynamic segment:

@app.route('/user/')
def profile(username):
    return f"User: {username}"

What does url_for('profile', username='alice') return?
A"/user/"
B"/user/<username>"
C"/user/alice"
D"/profile/alice"
Attempts:
2 left
💡 Hint
url_for replaces dynamic parts with the values you pass.
📝 Syntax
advanced
2:00remaining
Which url_for call raises a BuildError?
Given this route:

@app.route('/post/')
def show_post(id):
    return f"Post {id}"

Which url_for call will cause an error?
Aurl_for('show_post')
Burl_for('show_post', id=0)
Curl_for('show_post', id='10')
Durl_for('show_post', id=5)
Attempts:
2 left
💡 Hint
Check if all required parameters are provided.
🧠 Conceptual
advanced
2:00remaining
What does url_for generate with _external=True?
Given this route:

@app.route('/about')
def about():
    return 'About page'

What does url_for('about', _external=True) return if the app runs on http://localhost:5000?
A"/about"
B"http://localhost:5000/about"
C"https://localhost:5000/about"
D"localhost:5000/about"
Attempts:
2 left
💡 Hint
The _external flag adds the full URL including scheme and host.
🔧 Debug
expert
2:30remaining
Why does this url_for call produce a TypeError?
Examine this code snippet:

@app.route('/item/')
def item_detail(item_id):
    return f"Item {item_id}"

url = url_for('item_detail', item_id='abc')

Why does the url_for call raise a TypeError?
ABecause 'abc' is a string and cannot be converted to int for the route parameter
BBecause url_for requires all parameters to be integers
CBecause the route name 'item_detail' is incorrect
DBecause url_for cannot accept keyword arguments
Attempts:
2 left
💡 Hint
Check the type expected by the route converter and the type passed.