Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
175 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...