0
0
Flaskframework~30 mins

Blueprint URL prefixes in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Blueprint URL prefixes in Flask
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use app.register_blueprint(products_bp, url_prefix='/products') to register the products blueprint with the prefix.