-
-
Notifications
You must be signed in to change notification settings - Fork 16.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested blueprint should be able to have same name #4069
Comments
Should this really be possible? having |
The me the current situation was even more confusing, the error both uses the name Additionally, say this limitation stays in place, I'd need to rename the
That seems rather odd, having |
If you agree on this behavior, I'd be willing to create a PR for this. |
In my view each instance of a blueprint must have a unique name, as this allows flask to know which blueprint's methods to invoke. If you deleted the |
That would defeat the purpose of nested blueprints, as then to get I just did some inspection to see the logic here, but it seems my proposal was already included in the original https://github.com/pallets/flask/pull/3923/files#diff-30595964c383760a7295e59705f71f85766068b8231d8227ee34b5f5752a41e9R327. Nested blueprints already build their endpoint based on their parent blueprints, just not register in |
I think I understand what you are asking now. I think you'd like something like an from pprint import pprint
import flask
app = flask.Flask(__name__)
view_bp = flask.Blueprint("home", __name__)
api_bp = flask.Blueprint("api", __name__, url_prefix="/api")
home_api_bp = flask.Blueprint("api_home", __name__, endpoint="home")
@view_bp.route("/")
@home_api_bp.route("/")
def view():
print(flask.request, flask.request.endpoint)
api_bp.register_blueprint(home_api_bp)
app.register_blueprint(view_bp)
app.register_blueprint(api_bp)
pprint(app.url_map) giving this output,
Sadly the names must be unique and the name used in the endpoint must match the name used in the endpoint. It isn't clear to me how this could be altered as these conditions allow the endpoint to be split by |
This is getting into the territory of #1091. |
Given the comments the PR, would it be an option to have blueprints be initialized with a name keeping the requirement that the name shouldn't contain parent = flask.Blueprint("parent", __name__, url_prefix="/parent")
child = flask.Blueprint("child", __name__, url_prefix="/child")
grandchild = flask.Blueprint("grandchild", __name__, url_prefix="/grandchild")
@parent.route("/")
def parent_index():
print(request.endpoint) # parent.parent_index
print(request.blueprint) # parent
print(request.blueprints) # ['parent']
return "Parent"
@child.route("/")
def child_index():
print(request.endpoint) # parent.child.child_index
print(request.blueprint) # parent.child
print(request.blueprints) # ['parent.child', 'parent']
return "Child"
@grandchild.route("/")
def grandchild_index():
print(request.endpoint) # parent.child.grandchild.grandchild_index
print(request.blueprint) # parent.child.grandchild
print(request.blueprints) # ['parent.child.grandchild', 'parent.child', 'parent']
return "Grandchild" where
Using this the instances of parent can still be found by iteratively spliting on |
@pgjones and I have discussed this on Discord and know how we're going to solve it. That's why that PR was closed, another will be submitted. Basically |
Nested blueprints should be able to have the same name. Currently all blueprints are registered under their own name, however for a nested blueprint id expected it to use the
<parent_name>.<child_name>
Environment:
The text was updated successfully, but these errors were encountered: