Reputation: 2781
I'm setting values in a Flask session on one page, and retrieving the values on the next page. In between the two, the keys in the dictionary I'm using seem to get converted from integers to strings. Is this supposed to happen?
Example:
from flask import session, redirect
@app.route('/')
def page_1():
test = {}
test[1] = {}
print(test) # outputs >>> {1: {}}
session['store'] = {}
session['store'][1] = {}
return redirect('/two')
@app.route('/two')
def page_2():
print(session)
The output of the print looks like this (and querying the dict for key 1
returns an error, while '1'
works):
<SecureCookieSession {'store': {'1': {}}, 'csrf_token': 'biglongtoken...'}>
Is there any way around this? Is it supposed to happen?
Upvotes: 1
Views: 956
Reputation: 11131
When you create a session, whatever you store inside the session is securely signed (not encrypted) and sent as a cookie to the client, and when the client reconnects to your site, it sends the cookie back, you deserialize it and access using flask.session
.
Flask uses itsdangeous
to sign the session data, which serializes your data into JSON. In JSON, keys can only be strings, non-strings are converted. When reading the cookie from the client, the keys remain strings.
Upvotes: 3