If I nest blueprints such as:
a_bp = Blueprint('a', __name__)
b_bp = Blueprint('b', __name__)
c_bp = Blueprint('c', __name__)
b_bp.register_blueprint(c_bp, url_prefix="/c")
a_bp.register_blueprint(b_bp, url_prefix="/b")
app.register_blueprint(a_bp, url_prefix="/a")
how can I include a dynamic url identifier (a variable in the url, such as <int:variableName>
) for each level?
For example:
a_bp = Blueprint('a', __name__)
a_id_bp = Blueprint('id', __name__)
b_bp = Blueprint('b', __name__)
b_id_bp = Blueprint('id', __name__)
c_bp = Blueprint('c', __name__)
b_id_bp.register_blueprint(c_bp, url_prefix="/c")
b_bp.register_blueprint(b_id_bp, url_prefix="/<int:b_id>")
a_id_bp.register_blueprint(b_bp, url_prefix="/b")
a_bp.register_blueprint(a_id_bp, url_prefix="/<int:a_id>")
app.register_blueprint(a_bp, url_prefix="/a")
such that htmls appear as:
/a/<int:a_id>/b/<int:b_id>/c/
If I nest blueprints such as:
a_bp = Blueprint('a', __name__)
b_bp = Blueprint('b', __name__)
c_bp = Blueprint('c', __name__)
b_bp.register_blueprint(c_bp, url_prefix="/c")
a_bp.register_blueprint(b_bp, url_prefix="/b")
app.register_blueprint(a_bp, url_prefix="/a")
how can I include a dynamic url identifier (a variable in the url, such as <int:variableName>
) for each level?
For example:
a_bp = Blueprint('a', __name__)
a_id_bp = Blueprint('id', __name__)
b_bp = Blueprint('b', __name__)
b_id_bp = Blueprint('id', __name__)
c_bp = Blueprint('c', __name__)
b_id_bp.register_blueprint(c_bp, url_prefix="/c")
b_bp.register_blueprint(b_id_bp, url_prefix="/<int:b_id>")
a_id_bp.register_blueprint(b_bp, url_prefix="/b")
a_bp.register_blueprint(a_id_bp, url_prefix="/<int:a_id>")
app.register_blueprint(a_bp, url_prefix="/a")
such that htmls appear as:
/a/<int:a_id>/b/<int:b_id>/c/
Share
Improve this question
edited Mar 5 at 12:25
kando
asked Mar 4 at 15:13
kandokando
6051 gold badge7 silver badges19 bronze badges
4
|
1 Answer
Reset to default 1You can also use a dynamic rule for the url_prefix
attribute. When registering the blueprint, the individual rules are combined into one and passed to Flask.add_url_rule()
.
In the endpoint, it is important to note that all parameters defined by the assembled rule are also accepted as attributes.
from flask import Flask, Blueprint, request
from markupsafe import escape
app = Flask(__name__)
bp_a = Blueprint('a', __name__)
# You can set the attribute either when instantiating the blueprint.
bp_a_id = Blueprint('id', __name__, url_prefix='/<int:a_id>')
bp_b = Blueprint('b', __name__)
bp_b_id = Blueprint('id', __name__)
bp_c = Blueprint('c', __name__)
@bp_c.route('/')
def index(**kwargs):
return escape(f'{request.endpoint} = {request.url_rule} : {kwargs}')
bp_b_id.register_blueprint(bp_c, url_prefix='/c')
# Or you can set the attribute when registering the blueprint.
bp_b.register_blueprint(bp_b_id, url_prefix='/<int:b_id>')
bp_a_id.register_blueprint(bp_b, url_prefix='/b')
bp_a.register_blueprint(bp_a_id)
app.register_blueprint(bp_a, url_prefix='/a')
For example, calling http://localhost:5000/a/1/b/2/c/
produces the following output.
a.id.b.id.c.index = /a/<int:a_id>/b/<int:b_id>/c/ : {'a_id': 1, 'b_id': 2}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745036291a4607537.html
Flask.add_url_rule()
. It is important that your endpoint in the blueprint accepts all parameters. – Detlef Commented Mar 5 at 9:17