Python – Keeping a variable around from post to get


I have a class called myClass which defines post() and get() methods.

From index.html, I have a form with an action that calls which grabs some data from the data base, sets a couple variables and sends the user to new.html.

now, new.html has a form which calls myClass.get().

I want the get() method to know the value of the variables I got in post(). That is is main point here.

I figure the submit from new.html creates a separate instance of myClass created by the submit from index.html.

Is there a way to access the "post instance" somehow?

Is there a workaround for this? If I have to, is there an established way to send the value from post to "new.html" and send it back with the get-submit?

more generally, I guess I don't understand the life of my instances when web-programming. In a normal interactive environment, I know when the instance is created and destroyed, but I don't get that when I'm only using the class through calls to its methods. Are those classes even instantiated unless their methods are called?

Best Solution

What you're talking about is establishing a "session". That is, a way to remember the user and the state of their transaction.

There are several ways of tackling this, all of which rely on techniques for remembering that you're in a session in the first place.

HTTP provides you no help. You have to find some place to save session state on the server, and some place to record session identification on the client. The two big techniques are

  • Use a cookie to identify the session. Seamless and silent.

  • Use a query string in the URL to identify the session. Obvious because you have a ?sessionid=SomeSessionGUID in your URL's. This exposes a lot and makes bookmarking annoying. After the session is cleaned up, you still have this session id floating around in people's bookmarks.

  • In a limited way, you can also use hidden fields in a form. This only works if you have forms on every page. Not always true.

Here's how it plays out in practice.

  1. GET response. Check for the cookie in the header.

    a. No cookie. First time user. Create a session. Save it somewhere (memory, file, database). Put the unique ID into a cookie. Respond knowing this is their first time.

    b. Cookie. Been here recently. Try to get the cookie.

    • FInd the session object. Respond using information in the cookie.

    • Drat. No session. Old cookie. Create a new one and act like this is their first visit.

  2. POST response. Check for the cookie in the header.

    a. No cookie. WTF? Stupid kids. Get off my lawn! Someone bookmarked a POST or is trying to mess with your head. Respond as if it was a first-time GET.

    b. Cookie. Excellent. Get the cookie.

    • Find the session object. Find the thing you need in the session object. Respond.

    • Drat. No session. Old cookie. Create a new one and respond as if was a first-time GET.

You can do the same thing with a query string instead of a cookie. Or a hidden field on a form.