简体   繁体   中英

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

I create a context_process in blueprint apple,

apple > 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?

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

eg

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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