我完全不知道你所說的「不同的行爲」的意思,但如果你能找出共同的邏輯,那麼你可以使用這個:
# In your blueprint file
def get_blueprint(param):
blueprint = Blueprint('my blueprint', __name__)
@blueprint.route('/')
def index():
return "This is a common page."
if param == 'a':
@blueprint.route('/param')
def param():
return "This is the param a!"
elif param == 'b':
@blueprint.route('/param')
def param():
return "This is the param b!"
return blueprint
# In your app
app = Flask(__name__)
app.register_blueprint(get_blueprint('a'), url_prefix='/param/a')
app.register_blueprint(get_blueprint('b'), url_prefix='/param/b')
這樣,你能避免做一個if語句對於每個檢查url的請求。
是的,這是我的觀點#2,如果藍圖有很多路線做'@ blueprint.route'會讓'get_blueprint'非常大 – sashab