Flask-Session Type Error: 'NoneType' object is not subscriptable

503 views Asked by At

I am trying to use flask-session in my flask application but the session variable is giving a NoneType object each time. I looked through the documentation of Flask-Session and other answers but couldn't figure out the problem.

api.py

...
app.config["SESSION_PERMANENT"] = True
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=1)
app.config['SESSION_FILE_THRESHOLD'] = 5
app.config['SECRET_KEY'] = "F1"
SESSION_TYPE = "filesystem"
app.config.from_object(__name__)
sess = Session()
sess.init_app(app)
...
@app.route('/api/selectyear', methods=['POST'])
def selectyear():
    if request.method == 'POST':
        selected_year = request.get_json(force=True)
        session['selected_year'] = selected_year
        # do something and return
...

@app.route('/api/selectrace', methods=['POST'])
def selectrace():
    if request.method == 'POST':
        selected_year = session.get('selected_year', None)
        # use selected_year

When I try to use selected_year in the /api/selectrace route, it gives me a NoneType object. I tried reading on this issue and found this. This answer said this issue could be due to SESSION_PERMANENT being set to False but it persists for me even on setting it to True.

0

There are 0 answers