-1

I was trying to put

session['logged_in'] = True

in the session, but in another blueprint it doesn't persist... Why is that?

Is there any better way to keep something in the session?

Extended:

I have a blueprint giving a form to login. When done and submitted, it will set a session key like above. Then it redirects via

return redirect(url_for('admin.index'))

to admin page where If I call the key via

session.get('logged_in')

I get "None" Instead of the True or False one.

davidism
  • 121,510
  • 29
  • 395
  • 339
Fahad Ahammed
  • 371
  • 1
  • 11
  • 23

1 Answers1

2

I think I understand your confusion now~

Your flask session won't store anything on the server. the 'session' dict is filled by the cookies from the client request.

Again. that is:

client make login request to server, and got a [login success] response as well as a [cookies] which contains the !!!sessionINFO!!! you think are stored on the server side.

Next time, you must send the whole cookies to the server again, then your session in the server may have data.

Browser will do this for you. If you use a local client, say python requests library. Then be sure you are making requests with session (for requests-lib, it's requests.Session())

------------------OLD-------------------------------------

Though not an expert, but the case you described should not have happened.

The session is cookies data encrypted with a secret, if you have gone through the document mentioned by Beqa.

Just set

app.secret = '........'

And use session as a dict.

just FYI,

client request---->server (encrypt your_data 'logged_in' and client_relating_data 'maybe: ip, host or etc.', and put the encrypted info in cookies 'session=....') ------> client (get response with cookies)

client request again -----> server (decrypt the cookie 'session=...' with your secret), find the 'logged_in' data and know you are logged in.)

the cookies is something like below.

So, I'm not sure what's actually your trouble when using session, and put some basic information here. Just hope it helps in case.

enter image description here

Jilin
  • 291
  • 1
  • 2
  • 12
  • I have extended the question for better understanding. – Fahad Ahammed Mar 29 '18 at 06:40
  • Thanks, but the I am confused. In plain flask, I can do things with stored object in session to do things in several routes but in Blueprints, I am not getting this..... I can store anything: session['Location'] = "Tokyo" .... and can check If this same one is in another request.... – Fahad Ahammed Mar 30 '18 at 13:20
  • Your case is not detailed enough. I'm guessing for flask, your several requests has 'conincidentally' contained the needed cookies, while for blueprint test, you somehow requested without the cookies that previous reponse gave you. If you wanna know if your session data is stored,, just check the cookies in the response flask send back to you where session=xxx should be there. – Jilin Apr 02 '18 at 04:50