How to Use url_prefix with Blueprint in Flask
In Flask, you use
url_prefix when registering a Blueprint to add a common URL path prefix to all routes in that blueprint. This helps organize routes under a shared base path, for example, /admin for all admin-related routes.Syntax
The url_prefix is an optional argument used when registering a Blueprint with the Flask app. It defines a common URL path prefix for all routes in that blueprint.
Blueprint('name', __name__): Creates a blueprint instance.app.register_blueprint(blueprint, url_prefix='/prefix'): Registers the blueprint with a URL prefix.- All routes inside the blueprint will be accessed under
/prefix/route.
python
from flask import Flask, Blueprint bp = Blueprint('bp', __name__) @bp.route('/hello') def hello(): return 'Hello from blueprint!' app = Flask(__name__) app.register_blueprint(bp, url_prefix='/prefix')
Example
This example shows a Flask app with a blueprint registered using url_prefix='/admin'. The route /dashboard inside the blueprint will be accessible at /admin/dashboard.
python
from flask import Flask, Blueprint admin_bp = Blueprint('admin', __name__) @admin_bp.route('/dashboard') def dashboard(): return 'Admin Dashboard' app = Flask(__name__) app.register_blueprint(admin_bp, url_prefix='/admin') if __name__ == '__main__': app.run(debug=True)
Output
Running the app and visiting http://localhost:5000/admin/dashboard shows the text: Admin Dashboard
Common Pitfalls
- Not using
url_prefixwhen you want to group routes, causing route conflicts. - Forgetting the leading slash in
url_prefix(it must start with/). - Defining routes inside the blueprint without leading slashes can cause unexpected URLs.
- Registering the same blueprint multiple times with different prefixes can cause confusion.
python
from flask import Flask, Blueprint bp = Blueprint('bp', __name__) # Wrong: url_prefix missing leading slash # app.register_blueprint(bp, url_prefix='admin') # This will cause errors # Correct: app = Flask(__name__) app.register_blueprint(bp, url_prefix='/admin')
Quick Reference
Use url_prefix to group blueprint routes under a common path.
- Always start
url_prefixwith a slash (/). - Routes inside blueprint should start with a slash (
/). - Access routes as
url_prefix + route_path.
| Concept | Description | Example |
|---|---|---|
| Blueprint creation | Create a blueprint instance | bp = Blueprint('bp', __name__) |
| Route inside blueprint | Define routes with leading slash | @bp.route('/hello') |
| Register blueprint | Register with url_prefix starting with slash | app.register_blueprint(bp, url_prefix='/admin') |
| Access route | Combine prefix and route path | /admin/hello |
Key Takeaways
Use url_prefix when registering a blueprint to add a common URL path prefix to all its routes.
Always start url_prefix with a leading slash to avoid routing errors.
Define routes inside blueprints with leading slashes for consistent URLs.
url_prefix helps organize routes and prevent conflicts in larger Flask apps.