Access Flask config outside of application factory

后端 未结 1 1679
醉梦人生
醉梦人生 2020-12-10 22:18

I\'m currently using the Flask Application Factory pattern with Blueprints. The issue that I\'m having is how do I access the app.config object outside of the application fa

1条回答
  •  有刺的猬
    2020-12-10 22:59

    There is no need to use global names here, that defeats the purpose of using an app factory in the first place.

    Within views, such as in your example, current_app is bound to the app handling the current app/request context.

    from flask import current_app
    
    @bp.route('/')
    def example():
        servers = current_app.config['CUPS_SERVERS']
        ...
    

    If you need access to the app while setting up a blueprint, the record decorator marks functions that are called with the state the blueprint is being registered with.

    @bp.record
    def setup(state):
        servers = state.app.config['CUPS_SERVERS']
        ...
    

    0 讨论(0)
提交回复
热议问题