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 ??
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With