Complete worked example
python
from flask import Flask, Blueprint, url_for
def create_app(prefix):
app = Flask(__name__)
app.config['TESTING'] = True
roles = Blueprint('roles', __name__)
@roles.get('/<int:role_id>')
def detail(role_id):
return {'id': role_id}
app.register_blueprint(roles, url_prefix=prefix)
return app
def blueprint_path(prefix, role_id):
app = create_app(prefix)
with app.test_request_context():
path = url_for('roles.detail', role_id=role_id)
return {'path': path, 'status': app.test_client().get(path).status_code}- Reasoning
- The factory constructs a roles blueprint, defines a detail view and registers the group under the supplied prefix. URL generation uses roles.detail, not only detail.