On Tue, Apr 8, 2008 at 10:16 AM, Raoul Snyman <[EMAIL PROTECTED]> wrote:
>
>  On Tue, Apr 8, 2008 at 6:39 PM, Christoph Haas <[EMAIL PROTECTED]> wrote:
>  >  Your class variables are not saved between HTTP requests. If you want
>  >  data to persist you either need to save it in into a database (hint:
>  >  model) or into the session (cookie based session).
>
>  Just to further explain what Christoph has said, since I had the same
>  misconception as Dave when I started writing web apps...
>
>  In the traditional software model, once an object is created within
>  the application, it remains there until either it is destroyed or the
>  application is closed. In Web Apps, this is not the case. In essence,
>  each and every page that you go to is a single execution of your
>  application... from startup to shutdown. This means that while the
>  page is loading your object is valid, but as soon as the page has
>  finished loading, your object has been destroyed. So any variables you
>  set in your controller are not available on the next page or function
>  execution.

Well, yes, but not precisely.  A web app has several kinds of state
(scopes).  "Request" state lasts for the duration of the request.
Pylons instantiates a controller for every request; another framework
might use the same instance for every request.  So in Pylons the
'request' object and controller instance are request scope.

Session scope ties together several requests by the same user running
the same browser.  In Pylons the "session" object has session scope,
so anything you put into it is visible in later requests within the
session.  Normally the session ends when the user quits the browser,
but you can make it longer or shorter by setting the expiration time
of the session cookie, or by deleting the session data on disk (in
myapp/data/sessions).  So session scope sounds like what  you want.

Persistent scope lasts forever, or at least until you delete it or the
hard disk keels over and dies.  In Pylons the model -- tied to a SQL
database or such -- is persistent.  But here you must think about
whether the data should be visible to everybody, or to just the user
who created it.  To keep it private to the user, you'll have to put a
username field in the record, and set up authentication to find out
who the user is.  (You could also keep session data in the database,
though there's no reason to.  But if you wanted to, you'd key it by
session ID, which is "session.id" in Pylons.)

There are also several global scopes, which are shared by all requests
and users.  Pylons' "g" variable is one.  So is any module you import,
or any controller class, or any Python global.  Some of these are
shared between threads, while others may be thread local.  Ask on the
list if you're not sure if you're modifying something in a thread-safe
manner.  Global data disappears when the Pylons application quits.

Your original message and replies said "class variables".  Those would
be attributes attached to the controller class, not "self" variables
attached to the instance.  ('MyController.foo = "bar"' as opposed to
'self.foo = "bar"'.)  Class attributes are global scope; instance
attributes are request scope.

-- 
Mike Orr <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to