Challenge - 5 Problems
Blueprint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing the registered blueprint route?
Given the Flask app with a blueprint registered under '/shop', what will be the response when visiting '/shop/items'?
Flask
from flask import Flask, Blueprint shop_bp = Blueprint('shop', __name__) @shop_bp.route('/items') def items(): return 'Shop Items List' app = Flask(__name__) app.register_blueprint(shop_bp, url_prefix='/shop') # What is the output of a GET request to '/shop/items'?
Attempts:
2 left
💡 Hint
Think about how url_prefix affects blueprint routes.
✗ Incorrect
The blueprint 'shop_bp' is registered with the prefix '/shop'. The route '/items' inside the blueprint becomes '/shop/items' in the app. So accessing '/shop/items' returns 'Shop Items List'.
📝 Syntax
intermediate2:00remaining
Which option correctly creates and registers a blueprint?
Select the code snippet that correctly creates a Flask blueprint named 'admin' and registers it with the app under '/admin'.
Attempts:
2 left
💡 Hint
Check the Blueprint constructor and register_blueprint parameters.
✗ Incorrect
Blueprint requires two arguments: name and import name (__name__). The url prefix is set with url_prefix, not prefix or url.
🔧 Debug
advanced2:00remaining
Why does this blueprint route cause a 404 error?
Consider this code snippet:
from flask import Flask, Blueprint
api = Blueprint('api', __name__)
@api.route('/data')
def data():
return 'API Data'
app = Flask(__name__)
# Missing blueprint registration
# What happens when you visit '/data' and why?
Attempts:
2 left
💡 Hint
Think about what happens if you define a blueprint but don't register it.
✗ Incorrect
Blueprint routes only become active when the blueprint is registered with the Flask app. Without registration, the app does not know about '/data' route, so it returns 404.
❓ state_output
advanced2:00remaining
What is the output of this blueprint route using url variables?
Given the blueprint and route below, what is the output when visiting '/blog/post/42'?
Flask
from flask import Flask, Blueprint blog_bp = Blueprint('blog', __name__) @blog_bp.route('/post/<int:post_id>') def show_post(post_id): return f'Post ID: {post_id}' app = Flask(__name__) app.register_blueprint(blog_bp, url_prefix='/blog')
Attempts:
2 left
💡 Hint
Look at how url variables are passed to the route function.
✗ Incorrect
The route captures the integer 42 from the URL and passes it as post_id to the function, which returns 'Post ID: 42'.
🧠 Conceptual
expert3:00remaining
What happens if two blueprints have overlapping routes without url_prefix?
If you create two blueprints, both defining a route for '/' and register both without url_prefix, what will happen when you visit '/' in the Flask app?
Attempts:
2 left
💡 Hint
Think about how Flask handles route conflicts in registration order.
✗ Incorrect
Flask registers routes in the order blueprints are registered. If two routes have the same URL, the last registered route overrides the previous one silently.