简体   繁体   English

在 flask 上的应用程序工厂中初始化配置

[英]Initialize configuration in application factory on flask

I saw some sample codes, but I can't understand some part of these codes.我看到了一些示例代码,但我无法理解这些代码的某些部分。

Here is the content of sample codes:以下是示例代码的内容:

  • Flask application structure: Flask应用结构:
.
├── app
│   ├── __init__.py
└── config.py
  • config.py:配置文件:
    import os
    basedir = os.path.abspath(os.path.dirname(__file__))
    
    class Config:
        SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
        # ...
        
            @staticmethod
        def init_app(app):
            pass
    
    
    class DevelopmentConfig(Config):
        # ...
    
    
    class TestingConfig(Config):
        # ...
    
    
    class ProductionConfig(Config):
        # ...
    
    
    config = {
        'development': DevelopmentConfig,
        'testing': TestingConfig,
        'production': ProductionConfig,
        'default': DevelopmentConfig
    }
  • app/ init .py应用程序/初始化.py
    from flask import Flask, render_template
    from config import config
    # ...
    
    def create_app(config_name):
        app = Flask(__name__)
        app.config.from_object(config[config_name])
        config[config_name].init_app(app)
            # ...
        return app

I want to know the usage of these lines:我想知道这些行的用法:

  • config[config_name].init_app(app)
  •  def init_app(app): pass

If I ignore it, what would happen?如果我忽略它,会发生什么?

In flask, init_app is the common method to combine the FlaskApp and the third-libs在flask中,init_app是FlaskApp和third-libs结合的常用方法

If you don't want to do something, don't define the init_app method如果您不想做某事,请不要定义init_app方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM