I am now doing CS50W project 1. After letting user login and having enabled the user to do the book search, I need a way to keep track of the user when he or she is browsing from one page to another. I wonder how I can do that in python. In java that I am used to, I will set up a static variable from the start and set the value after the user login and the same variable can be passed from one function to the other. Is there any equivalent in python?

Thanks. Hoi Fei


You can do that by using Flask sessions. Brian introduces the concept at 1:40 of the Flask Lecture. The distro code includes the necessary configuration.

  • I used the distro code and set up a variable at the beginning called CURRENT_USER and then update it inside one of the @app.route after user has logged in. However, I found that the value of the CURRENT_USER was back to default after it has gone to another route. In the end I solved the problem by setting up a user object at the beginning and update the id of the user object after the user has logged in. – Hoi Fei Kwok Apr 9 '20 at 14:26
  • That's not using session as described in the lecture. – DinoCoderSaurus Apr 9 '20 at 14:42
  • Thanks. I have now used the session and it works – Hoi Fei Kwok Apr 11 '20 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .