The flask object implements a WSGI application and acts as the central object. It is passed the name of the module or package of the application. Once it is created it will act as a central registry for the view functions, the URL rules, template configuration and much more.
The name of the package is used to resolve resources from inside the package or the folder the module is contained in depending on if the package parameter resolves to an actual python package (a folder with an :file:`__init__.py` file inside) or a standard module (just a ``.py`` file).
Usually you create a :class:`Flask` instance in your main module or in the :file:`__init__.py` file of your package like this:
from flask import Flask
app = Flask(__name__)
The idea of the first parameter is to give Flask an idea of what belongs to your application. This name is used to find resources on the filesystem, can be used by extensions to improve debugging information and a lot more.
So it's important what you provide there. If you are using a single module, `__name__` is always the correct value. If you however are using a package, it's usually recommended to hardcode the name of your package there.
以上内容粘贴自2.2.2版本Flask源码的说明部分(有部分删改)
本文介绍了如何创建Flask应用实例,如何设置应用名称以正确定位资源,以及包名与模块路径的区别。重点在于理解`Flask`对象的作用和配置参数选择的重要性。
4189

被折叠的 条评论
为什么被折叠?



