python flask, keeping session in scope using another class python flask, keeping session in scope using another class flask flask

python flask, keeping session in scope using another class


The session variable from Flask is a context local. Context locals are a somewhat special type of global variable, in that they are only accessible at certain times.

Specifically for session, you can only use it when the application is responding to a request. Flask sets the value of this variable right before invoking a view function.

In your stack trace, it appears you are creating an instance of the Security class during initialization. The constructor for this class accesses session, but at this time, there is really no client, no request, and no session, so this gives you an error. It's okay to create an object of this class, as long as you don't access the session in the constructor. Then you must make sure that you invoke the loggedIn method only inside a view function or a function called from a view function, to ensure a request context exists.