I would like to store in the Flask session variables an object unique to each user of my website.
The naive solution would be to check in every route that uses this object that it has been correctly initialized in the session variables, and if it hasn't, create it. But I wonder if there is a decorator (like @before_first_request
) or some other mechanism that allows to execute code when a new session is created, so that there is no need to check that the object has been created beforehand.
Thank you for your help.
Sounds like a job for before_request()
:
@app.before_request
def before_request():
if current_user.is_active:
#edit or add your session var
return None