0
0
Flaskframework~20 mins

Blueprint URL prefixes in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Blueprint URL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the URL for the route in this Flask blueprint?

Given a Flask blueprint with a URL prefix, what URL will respond to the hello route?

Flask
from flask import Flask, Blueprint

bp = Blueprint('bp', __name__, url_prefix='/api')

@bp.route('/hello')
def hello():
    return 'Hello from blueprint!'

app = Flask(__name__)
app.register_blueprint(bp)
A/hello
B/api/hello
C/bp/hello
D/api
Attempts:
2 left
💡 Hint

Remember the url_prefix adds a prefix to all routes in the blueprint.

state_output
intermediate
2:00remaining
What is the output when accessing the root URL of this blueprint?

Consider this Flask blueprint with a URL prefix. What response will the client get when accessing /dashboard/?

Flask
from flask import Flask, Blueprint

dashboard = Blueprint('dashboard', __name__, url_prefix='/dashboard')

@dashboard.route('/')
def index():
    return 'Dashboard Home'

app = Flask(__name__)
app.register_blueprint(dashboard)
AInternal Server Error
B404 Not Found
CDashboard Home
DMethod Not Allowed
Attempts:
2 left
💡 Hint

The route is defined at / inside the blueprint with prefix /dashboard.

📝 Syntax
advanced
2:30remaining
Which option correctly sets a blueprint with a URL prefix and a route?

Choose the code snippet that correctly creates a Flask blueprint with URL prefix /shop and a route /items that returns 'Shop Items'.

A
bp = Blueprint('shop', __name__, url_prefix='/shop')
@bp.route('/items')
def items():
    return 'Shop Items'
B
bp = Blueprint('shop', __name__)
@bp.route('/shop/items')
def items():
    return 'Shop Items'
C
bp = Blueprint('shop', __name__, url_prefix='shop')
@bp.route('items')
def items():
    return 'Shop Items'
D
bp = Blueprint('shop', __name__, url_prefix='/shop')
@bp.route('items')
def items():
    return 'Shop Items'
Attempts:
2 left
💡 Hint

URL prefixes and routes must start with a slash / to be valid.

🔧 Debug
advanced
2:30remaining
Why does this blueprint route return 404 despite being registered?

Given this Flask app and blueprint, why does accessing /blog/post return 404?

Flask
from flask import Flask, Blueprint

blog = Blueprint('blog', __name__, url_prefix='/blog')

@blog.route('post')
def post():
    return 'Blog Post'

app = Flask(__name__)
app.register_blueprint(blog)
AThe route decorator is missing a leading slash in 'post', so the route is invalid.
BThe blueprint is not registered with the app.
CThe URL prefix '/blog' is incorrect and should be '/blog/'.
DThe function 'post' is missing a return statement.
Attempts:
2 left
💡 Hint

Check the route string in the decorator for correct syntax.

🧠 Conceptual
expert
3:00remaining
How does Flask combine multiple blueprints with overlapping URL prefixes?

Suppose you register two blueprints with the same URL prefix /api. How does Flask handle routing when both define a route /status?

AFlask merges the two routes and calls both functions sequentially on access.
BFlask raises an error during app startup due to duplicate routes.
CBoth routes are accessible under different URLs automatically to avoid conflict.
DThe last registered blueprint's route overrides the previous one for <code>/api/status</code>.
Attempts:
2 left
💡 Hint

Think about how Flask registers routes internally and what happens if two routes have the same URL.