pythonflask configlist.py_flask config笔记

#从flask这个包中导入Flask这个类#Flask这个类是项目的核心,以后很多操作都是基于这个类的对象#注册url、注册蓝图等都是基于这个类的对象

from flask importFlaskimport config #配置文件

#创建一个Flask对象,传递__name__参数进去#__name__参数的作用:#1.可以规定模板和静态文件的查找路劲#2.以后一些Flask插件,比如Flask-migrate、Flask-SQLAlchemy如果报错了,#那么Flask可以通过这个参数找到具体的报错位置

app = Flask(__name__)#app.debug = True #第二中模式debug

#app.config.update(DEBUG=True)#第三种模式#可以看出config其实是一个字典

app.config.from_object(config)#第四种模式,从配置文件中导入

#为什么config的debug必须是大写

我们看下

from_object的源码

def from_object(self, obj):

"""Updates the values from the given object. An object can be of one

of the following two types:

- a string: in this case the object with that name will be imported

- an actual object reference: that object is used directly

Objects are usually either modules or classes. :meth:`from_object`

loads only the uppercase attributes of the module/class. A ``dict``

object will not work with :meth:`from_object` because the keys of a

``dict`` are not attributes of the ``dict`` class.

Example of module-based configuration::

app.config.from_object('yourapplication.default_config')

from yourapplication import default_config

app.config.from_object(default_config)

You should not use this function to load the actual configuration but

rather configuration defaults. The actual config should be loaded

with :meth:`from_pyfile` and ideally from a location not within the

package because the package might be installed system wide.

See :ref:`config-dev-prod` for an example of class-based configuration

using :meth:`from_object`.

:param obj: an import name or object

"""

if isinstance(obj, string_types):

obj = import_string(obj)

for key in dir(obj):#这里用dir()内置函数查看文件参数

if key.isupper():#这里判断了必须大写

self[key] = getattr(obj, key)这里获取debug的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值