首页 > 在flask中,怎样在 blueprint 中调用配置文件中的参数?

在flask中,怎样在 blueprint 中调用配置文件中的参数?

我把一个参数写进了flask项目的配置文件里,然后希望在 blueprint 中获取这个参数,但是似乎flask并没有提供Flask对象和blueprint 进行数据传递的接口?对这种问题有其他更好的解决办法吗?谢谢!


用全局g应该可以吧, http://flask.pocoo.org/docs/0.10/api/#flask.g
还有current_app

我试了下current_app,可以啊

目录

test
--app
  --__init__.py
  --home.py
--instance
  --config.py
--run.py

# app/__init__.py
from flask import Flask

from .home import home

def create_app():
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_pyfile('config.py')

    app.register_blueprint(home)
    return app



# app/home.py
from flask import Blueprint, current_app


home = Blueprint('home', __name__)


@home.route('/')
def helle():
    return 'hello home '+current_app.config['TEST']




# instance/config.py

# capitalize
DEBUG=True
TEST='wo'



# run.py
from app import create_app

app = create_app()
app.run(debug=app.config['DEBUG'])

不知道怎么从blueprint中获取参数,所以很好奇你是遇到了什么问题让你需要从blueprint中获取参数,或许不用从blueprint中获取参数也能解决。

【热门文章】
【热门文章】