flask: url_for can't distinguish a blueprint mounted two times

Based on blueprint concept, I expected it to handle relative url_for nicely:

from flask import Blueprint, Flask, url_for

bp = Blueprint('foo', __name__)

@bp.route('/')
def func():
    return url_for('.func')

app = Flask(__name__)
app.register_blueprint(bp, url_prefix='/foo')
app.register_blueprint(bp, url_prefix='/bar')

client = app.test_client()
print client.get('/foo/').data
print client.get('/bar/').data

Both prints write the URL to the first blueprint registered (/foo/). Is it possible to mount two times the same blueprint and make relative url_for work? Is this behaviour expected?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 2
  • Comments: 20 (5 by maintainers)

Most upvoted comments

This is really confusing. The same Register a blueprint multiple times on an application with different URL rules line in the docs let me to expect that utilities like url_for would still work, but they don’t.

My solution was to just nest my blueprint creation and route definition inside a make_blueprint(blueprint_name) factory function that could pass different name parameters to each Blueprint instance …

@jackunion we know it doesn’t work, but as per the docs it should work!

@jackunion the point of this issue is that it is a bug.

Yes. The docs give registering a single blueprint multiple times on the same app as an “intended use case”.

The utility of that is somewhat questionable if url_for doesn’t work properly.