Blueprint URL prefixes help organize your web app by grouping routes under a common starting path. This keeps URLs tidy and your code easier to manage.
You want to group related pages, like all user pages under '/users'.
You are building a big app and want to split routes into smaller parts.
You want to avoid repeating the same URL part for many routes.
You want to create reusable route groups that can be added with different prefixes.
This Flask app uses a blueprint with the prefix '/blog'. The route '/' inside the blueprint is accessed at '/blog/'. The route '/post/<id>' is accessed at '/blog/post/<id>'.
Flask
from flask import Flask, Blueprint
app = Flask(__name__)
# Create a blueprint with URL prefix '/blog'
blog_bp = Blueprint('blog', __name__, url_prefix='/blog')
@blog_bp.route('/')
def blog_home():
return'Welcome to the Blog Home'
@blog_bp.route('/post/<int:id>')
def show_post(id):
return f'Post number {id}'# Register blueprint with the app
app.register_blueprint(blog_bp)
if __name__ == '__main__':
app.run(debug=True)
OutputSuccess
Important Notes
URL prefixes help keep your app routes organized and avoid conflicts.
You can register the same blueprint multiple times with different prefixes if needed.
Summary
Blueprint URL prefixes add a common path to all routes in a blueprint.
This makes your URLs cleaner and your code easier to maintain.
Use them to group related routes logically under one URL section.
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]