python - 关于flask的g问题
PHP中文网
PHP中文网 2017-04-17 17:28:21
0
4
390

我在登陆的时候 自己写的login_user 里 将 user 对象保存在 g中,g.user = user 然后刷新了页面,但是当页面刷新后,g中就没有user了,请问怎么保存user在g中呢?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
Ty80

To share data that is valid for one request only from one function to another, a global variable is not good enough because it would break in threaded environments. Flask provides you with a special object that ensures it is only valid for the active request and that will return different values for each request. In a nutshell: it does the right thing, like it does for request and session.

你应该用session:http://flask.pocoo.org/docs/0.10/quickstart/#sessions

巴扎黑

This is a question about flask session persistence.
You can refer to here first: http://docs.jinkan.org/docs/flask/quickstart.html#sessions

左手右手慢动作

The g object is a shared variable in a request, and different requests correspond to different g objects. In this case, you should use session. The session object is used to share variables between different requests. The most common one is to implement the login and logout function.

洪涛

g is to share data between the program context and request context of a request. When you request the second time, g will not be the previous one. So you should use session or cookie

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template