Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask: getting url_prefix for blueprint in view

Tags:

python

flask

I have a blueprint with one view. I would like to get url_prefix of blueprint inside view. Unfortunately test.url_prefix returns None. Is there any other way?

app.register_blueprint(test_blueprint, url_prefix = "/test")

@test.route("/task", methods=["GET"])
def task_view(user):
    task_url = test.url_prefix + "/task" # test.url_prefix is None ??
like image 980
hakubaa Avatar asked Nov 23 '25 08:11

hakubaa


1 Answers

Yes.

In Flask the route path of the current view is contained in the url_rule.rule subproperty of the request variable.

So you can do the following:

from flask import request

...

test_blueprint = Blueprint('test', __name__, url_prefix='/test')

...

@test_blueprint.route("/task", methods=["GET"])
def task_view(user):
    task_url = request.url_rule.rule

....

app.register_blueprint(test_blueprint)

The value of task_url will be:

/test/task

as desired.

like image 146
David Simic Avatar answered Nov 24 '25 22:11

David Simic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!