Python 烧瓶,蓝印,当前应用

Python 烧瓶,蓝印,当前应用,python,flask,jinja2,Python,Flask,Jinja2,我试图在jinja环境中添加一个蓝图中的函数(我将在模板中使用该函数) Main.py app = Flask(__name__) app.register_blueprint(heysyni) heysyni = Blueprint('heysyni', __name__) @heysyni.route('/heysyni'): return render_template('heysyni.html',heysini=res_heysini) from flask import

我试图在jinja环境中添加一个蓝图中的函数(我将在模板中使用该函数)

Main.py

app = Flask(__name__)
app.register_blueprint(heysyni)
heysyni = Blueprint('heysyni', __name__)
@heysyni.route('/heysyni'):
    return render_template('heysyni.html',heysini=res_heysini)
from flask import Flask
from Ablueprint import heysyni

app = Flask(__name__)
app.register_blueprint(heysyni)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(debug=True)
from flask import Blueprint, current_app

heysyni = Blueprint('heysyni', __name__)

@heysyni.route('/heysyni/')
def aheysyni():
    #Got my app here
    app = current_app._get_current_object()
    return 'hello'
MyBluePrint.py

app = Flask(__name__)
app.register_blueprint(heysyni)
heysyni = Blueprint('heysyni', __name__)
@heysyni.route('/heysyni'):
    return render_template('heysyni.html',heysini=res_heysini)
from flask import Flask
from Ablueprint import heysyni

app = Flask(__name__)
app.register_blueprint(heysyni)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(debug=True)
from flask import Blueprint, current_app

heysyni = Blueprint('heysyni', __name__)

@heysyni.route('/heysyni/')
def aheysyni():
    #Got my app here
    app = current_app._get_current_object()
    return 'hello'
现在在MyBluePrint.py中,我想添加如下内容:

def role_function():
    return 'admin'
app.jinja_env.globals.update(role_function=role_function)
然后,我将能够在我的模板中使用此函数。我无法确定如何访问该应用程序,因为

app = current_app._get_current_object()
返回错误

working outside of request context

如何实现这种模式?

消息错误实际上非常清楚:

在请求上下文之外工作

在我的蓝图中,我试图让我的应用程序脱离“请求”功能:

heysyni = Blueprint('heysyni', __name__)

app = current_app._get_current_object()
print app

@heysyni.route('/heysyni/')
def aheysyni():
    return 'hello'
我只需添加,即可将当前的_app语句移动到函数中。最后,它是这样工作的:

Main.py

app = Flask(__name__)
app.register_blueprint(heysyni)
heysyni = Blueprint('heysyni', __name__)
@heysyni.route('/heysyni'):
    return render_template('heysyni.html',heysini=res_heysini)
from flask import Flask
from Ablueprint import heysyni

app = Flask(__name__)
app.register_blueprint(heysyni)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(debug=True)
from flask import Blueprint, current_app

heysyni = Blueprint('heysyni', __name__)

@heysyni.route('/heysyni/')
def aheysyni():
    #Got my app here
    app = current_app._get_current_object()
    return 'hello'
Ablueprint.py

app = Flask(__name__)
app.register_blueprint(heysyni)
heysyni = Blueprint('heysyni', __name__)
@heysyni.route('/heysyni'):
    return render_template('heysyni.html',heysini=res_heysini)
from flask import Flask
from Ablueprint import heysyni

app = Flask(__name__)
app.register_blueprint(heysyni)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(debug=True)
from flask import Blueprint, current_app

heysyni = Blueprint('heysyni', __name__)

@heysyni.route('/heysyni/')
def aheysyni():
    #Got my app here
    app = current_app._get_current_object()
    return 'hello'

但是当前的应用程序可以直接在函数中使用,为什么我需要使用_get_current_object()?