#从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的值