Skip to main content
 首页 » 编程设计

python之重用 Flask 蓝图 : extend or rewrite

2025年05月04日16bonelee

我是 Flask 蓝图的新手,我想知道实现可重用蓝图的最佳实践是什么,例如 flask-social-blueprint或 flask-users-blueprint ?我应该:

  • 将代码复制粘贴到我的项目中并覆盖我想要更改的任何内容,或者
  • 从蓝图导入,然后在单独的模块中编写覆盖函数?

换句话说,蓝图是指让您免于打字的样板文件,还是像 Flask“扩展”和其他无需更改原始代码即可导入的 Python 模块?

请您参考如下方法:

这取决于项目,但如果它是一个实际的库而不仅仅是一个示例,那么您应该将其添加为依赖项、导入蓝图等,然后将其注册到您的应用程序中,根据需要覆盖行为。蓝图是应用程序的扩展,而不是像(例如)HTML5 样板那样的可下载代码片段。

引自the documentation :

A blueprint in Flask is ... a set of operations which can be registered on an application, even multiple times. The basic concept of blueprints is that they record operations to execute when registered on an application.