Issue
having an issue with my flask app, where im trying to grab data from session storage however flask does not seem to detect that it exists.
def create_routine():
if session.get('routine') is not None: //<---routine does not appear to exist yet
try:
print("A routine was recently created")
obj = session['routine']
new_routine = WorkoutRoutines(
name=obj.name,
description=obj.description,
exercises=obj.exercises
)
db.session.add(new_routine)
db.session.commit()
session.pop('routine')
print("it has been successfully added to database")
return redirect(url_for('showWorkoutRoutines'))
except Exception as e:
print("something went wrong: ")
print(e)
else:
print("no routines have been created")
my guess is that my function is trying to access routine quicker than the browser can load the data for it, so it doesn't appear to exist when accessed. However im not sure how to resolve this
Solution
The issue is that a Flask session has nothing to do with a browser's Session Storage.
Flask sessions are cookies that contain an object (key/value map) that are encrypted and stored in the browser between requests. From the browser, it is just an opaque cookie with encrypted content and you can't access its contents from Javascript (by design).
Session Storage is a Javascript API - more info here:
https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
That API has nothing to do with cookies.
So, the issue is that it is two different session-related storage technologies and containers that don't interact in anyway.
Answered By - David K. Hess
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.