Flask的__name__的作用

本文介绍了如何创建Flask应用实例,如何设置应用名称以正确定位资源,以及包名与模块路径的区别。重点在于理解`Flask`对象的作用和配置参数选择的重要性。

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源码的说明部分(有部分删改)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值