Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

python - Flask Blueprint Putting something on session

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...