Where do I put my blueprint before_request

I'm even later here, but incrementing on Johnston's answer, you could use the same before_request decorator as well, for example:

bp_v1 = Blueprint('api_v1', __name__)

@bp_v1.before_request
def before_anything():
    pass

A little late here but:
This is what I do:
Use the Blueprint variable to set the before request

myblueprint = Blueprint('myblueprint', __name__, template_folder="templates")

def before_myblueprint():
    #code here

myblueprint.before_request(before_myblueprint)

I think you doing it good by trying initiate user in before_request, the problem is that g object has nothing before the request, so you need to deal with it differently. Get the user from cookies in before_request most probably and then later add it to session, from there maybe to g. I think it would be worth to take a look at or use Flask-login. Or just read the code of it and maybe it will give you some ideas.


Blueprint.before_request is called before each request within the blueprint. If you want to call it before all blueprints, please use before_app_request.

Tags:

Python

Flask