简体   繁体   English

Flask:如何在另一个蓝图中使用一个蓝图中的 context_processor

[英]Flask: How can i use a context_processor from one blueprint in another blueprint

I create a context_process in blueprint apple,我在蓝图苹果中创建了一个 context_process,

apple > views.py苹果> views.py

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

If i were in another blueprint, how would i access @apple.context_processor, so that I can use the variable when i render a template?如果我在另一个蓝图中,我将如何访问@apple.context_processor,以便在渲染模板时可以使用该变量?

Instead of assigning it to the blueprint, assign it to the app.不是将其分配给蓝图,而是将其分配给应用程序。

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

The whole point of having a blueprint-local context processor is that it only operates on that blueprint.拥有蓝图本地上下文处理器的全部意义在于它仅在该蓝图上运行。 So if that's not what you want, put it on the app.所以如果这不是你想要的,把它放在应用程序上。

You can use Bluepint.app_context_processor您可以使用 Bluepint.app_context_processor

eg例如

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

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

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

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