Flask: Dynamic url_prefix - Stack Overflow

If I nest blueprints such as:a_bp = Blueprint('a', __name__)b_bp = Blueprint('b',

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
  • Your example works as expected. What's the problem? – Detlef Commented Mar 4 at 20:42
  • I was under the impression that variables in the url could not be submitted as url_prefix – kando Commented Mar 5 at 2:53
  • The rules are combined into one and passed to Flask.add_url_rule(). It is important that your endpoint in the blueprint accepts all parameters. – Detlef Commented Mar 5 at 9:17
  • (Updated question for clarity.) – kando Commented Mar 5 at 12:27
Add a comment  | 

1 Answer 1

Reset to default 1

You 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: Dynamic url_prefix - Stack Overflow

    If I nest blueprints such as:a_bp = Blueprint('a', __name__)b_bp = Blueprint('b',

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信