1

I am trying to print the session id in my flask application using the below code:

@application.route("/")
def login():
    sid=session['_id']
    print(sid)
    return render_template("login.html")

However, I get a KeyError saying that _id is not valid. How can I print the session Id? Note: I have already set the secret key

Edit: My question is different from this question because:

  • I am not using flask login
  • There are no accepted answers to that question
  • The suggested answers don't work for me and I have already mentioned the error I got trying out one of their suggestion
  • I don't want to generate a unique session id. Rather I just want to access the session id which is automatically generated for every session.
codingsplash
  • 4,785
  • 12
  • 51
  • 90
  • above should work, but only if you've set the `_id` key of session yourself. there is no "default" session ID in flask: https://stackoverflow.com/questions/15156132/flask-how-to-get-session-id – abigperson Jul 13 '17 at 12:08
  • unless you actually add something to a user's session, the session won't be created so you won't have a session id. also you have to make sure to send back the same cookie to the server – Anis Jonischkeit Jul 28 '17 at 02:40

0 Answers0