📖 Scenario: You are building a small web app with two sections: one for users and one for products. Each section should have its own URL prefix to keep things organized.
🎯 Goal: Create two Flask blueprints named users_bp and products_bp. Register them in the main app with URL prefixes /users and /products respectively. Each blueprint should have one route that returns a simple message.
📋 What You'll Learn
Create a Flask app instance named app
Create a blueprint named users_bp with a route /profile that returns 'User Profile'
Create a blueprint named products_bp with a route /list that returns 'Product List'
Register users_bp with the URL prefix /users
Register products_bp with the URL prefix /products
💡 Why This Matters
🌍 Real World
Blueprints help organize large Flask apps by grouping related routes under URL prefixes, making the app easier to maintain.
💼 Career
Understanding blueprints and URL prefixes is essential for backend web developers working with Flask to build scalable web applications.
Progress0 / 4 steps
1
Create Flask app and users blueprint
Create a Flask app instance called app. Then create a blueprint called users_bp with the name 'users' and import name __name__. Add a route /profile to users_bp that returns the string 'User Profile'.
Flask
Hint
Use Flask(__name__) to create the app. Use Blueprint('users', __name__) to create the users blueprint. Define a function profile decorated with @users_bp.route('/profile') that returns 'User Profile'.
2
Create products blueprint with list route
Create a blueprint called products_bp with the name 'products' and import name __name__. Add a route /list to products_bp that returns the string 'Product List'.
Flask
Hint
Create the products blueprint like the users one. Add a route /list that returns 'Product List'.
3
Register users blueprint with URL prefix
Register the users_bp blueprint with the Flask app app using the URL prefix /users.
Flask
Hint
Use app.register_blueprint(users_bp, url_prefix='/users') to register the users blueprint with the prefix.
4
Register products blueprint with URL prefix
Register the products_bp blueprint with the Flask app app using the URL prefix /products.
Flask
Hint
Use app.register_blueprint(products_bp, url_prefix='/products') to register the products blueprint with the prefix.
Practice
(1/5)
1. What is the main purpose of using a URL prefix in a Flask blueprint?
easy
A. To add a common path segment to all routes in the blueprint
B. To change the HTTP methods allowed for the routes
C. To register the blueprint with the Flask app
D. To define the template folder for the blueprint
Solution
Step 1: Understand what URL prefix means in Flask blueprints
A URL prefix is a string added before all routes in a blueprint to group them under a common path.
Step 2: Identify the purpose of URL prefix
This helps organize routes logically and keeps URLs clean by adding a shared path segment.
Final Answer:
To add a common path segment to all routes in the blueprint -> Option A
Quick Check:
URL prefix = common path for blueprint routes [OK]
Hint: URL prefix groups routes under one shared path [OK]
Common Mistakes:
Confusing URL prefix with HTTP methods
Thinking URL prefix registers the blueprint
Mixing URL prefix with template folder settings
2. Which of the following is the correct way to set a URL prefix when creating a Flask blueprint?
easy
A. bp = Blueprint('bp', __name__, urlprefix='/admin')
B. bp = Blueprint('bp', __name__, prefix_url='/admin')
C. bp = Blueprint('bp', __name__, url_prefix='/admin')
D. bp = Blueprint('bp', __name__, route_prefix='/admin')
Solution
Step 1: Recall the correct parameter name for URL prefix
The Flask Blueprint constructor uses the parameter named 'url_prefix' to set the prefix.
Step 2: Check each option for correct syntax
Only bp = Blueprint('bp', __name__, url_prefix='/admin') uses 'url_prefix' exactly as Flask expects.
Final Answer:
bp = Blueprint('bp', __name__, url_prefix='/admin') -> Option C
Quick Check:
Correct parameter name is url_prefix [OK]
Hint: Use 'url_prefix' exactly when creating blueprint [OK]