Home > OS >  KeyError: 'user_available'
KeyError: 'user_available'

Time:03-07

I am trying to implement a check that if a user is available then render a specific template. I am using this condition if session['user_available']: where session is imported from flask. But I am getting KeyError: 'user_available' error. If this method is deprecated then what is the updated code for assessing the availability of current user using session.

CodePudding user response:

At the time of the request, the key-value pair does not exist in the session. This results in the KeyError.
Much of this session is to be used like a dict. So you can query if a key-value pair exists within the session. This should assume that even if the key is not part of the session, no KeyError is thrown.

if 'user_available' in session and session['user_available']:
    # The key is present in the session and the value is set.
  • Related