Flask:我如何在另一个蓝图

时间:2016-05-04 15:25:35

标签: python flask

我在blueprint apple中创建了一个context_process,

apple> views.py

@apple.context_processor
def eat_apple():
    return dict(fruit='apple')

如果我在另一个蓝图中,我将如何访问@ apple.context_processor,以便在渲染模板时可以使用该变量?

2 个答案:

答案 0 :(得分:1)

不是将其分配给蓝图,而是将其分配给应用程序。

@app.context_processor
def eat_apple():
    return dict(fruit='apple')

拥有蓝图 - 本地上下文处理器的整点是它只在该蓝图上运行。所以如果那不是你想要的,就把它放在应用程序上。

答案 1 :(得分:1)

您可以使用Bluepint.app_context_processor

例如

bp = Blueprint("myblueprint", __name__, url_prefix=None)                               

@myblueprint.app_context_processor                                                        
def inject_template_globals():                                                   
    company = Company.query.first()                                              
    return dict(company=company)