librelist archives

« back to archive

before_request context injection

before_request context injection

From:
Adam Patterson
Date:
2011-06-30 @ 02:37
Is this the recommended way to achieve context injection for an entire
blueprint?

@bp.before_request
def inject_context():
    def inject_object_count():
        object_count = Model.query.filter_by(user=current_user).count()
        return dict(object_count=object_count)

current_app.template_context_processors[None].append(inject_object_count)

I have many views but and I don't like specifying
@app.context_processor over all of them. It seems ok but I just want to make
sure there isn't something terribly wrong with doing this or that there
isn't a better way.

Thanks