# 定义蓝图example =Blueprint('example',__name__,url_prefix="/example")# 绑定视图函数# 在此处,完整的url是和蓝图的url_prefix组合在一起,为/example/auth@example.route('/auth')defauth(auth):return"auth is ok"# 注册路由app =Flask(__name__)app.register_blueprint(example_api)
我们来详细看看这背后到底发生了什么?先看看Flask类的register_blueprint函数:
classFlask(_PackageBoundObject):@setupmethoddefregister_blueprint(self,blueprint,**options): first_registration =Falseif blueprint.name inself.blueprints:assertself.blueprints[blueprint.name]is blueprint,("A name collision occurred between blueprints %r and %r. Both"' share the same name "%s". Blueprints that are created on the'" fly need unique names."%(blueprint,self.blueprints[blueprint.name], blueprint.name))else:self.blueprints[blueprint.name]= blueprintself._blueprint_order.append(blueprint) first_registration =True blueprint.register(self, options, first_registration)