search

Home  >  Q&A  >  body text

python - 关于flask的g问题

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

PHP中文网PHP中文网2901 days ago421

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:30:21

    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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:30:21

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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:30:21

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:30:21

    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

    reply
    0
  • Cancelreply