nadermx nadermx - 8 months ago 45
Python Question

Check if user is anonymous for Flask-Cache `unless` argument

I am trying to cache a view unless the user is logged in.

only works inside a view though, so I'm having trouble passing it to
. How do I do this correctly?

@app.cache.cached(timeout=300, unless=current_user.is_authenticated())
def index():
return 'stuff to return'


Wrap the call in a lambda. Flask-Cache will call the function when the view is executed, rather than once when the view is defined.

@app.cache.cached(timeout=300, unless=lambda: current_user.is_authenticated)

The docs specifically say that unless should be a callable that will be executed each time.