views:

36

answers:

2

I understand request.sessions dictionary and how to use this.

However, it appears that values set using request.sessions is only valid while the user is logged in.

I need to set a persistent cookie that lasts for a fixed time period and not dependent on whether the user is logged in or not.

What I would like is to store a value for an anonymous visitor to my site, and also retrieve that same value if that user creates an account and logs into the site. The value should be retriEvable if the user logs in or logs out between sessions.

Any code examples on this?

+2  A: 

Sessions should work fine for anonymous users. What's happening to make you think it only works for authenticated users?

Aside from that, maybe take a look at deferred registration which may do what you're looking for, http://tartarus.org/james/diary/2009/07/24/implementing-deferred-registration-with-django .

Rolo
Thanks rolo! The link was great. I've been spending the afternoon implementing a solution using this.I ran into a bit of trouble when I failed to realize that the .get_stashed_in_session method returns a LIST of objects and not the object itself, but I eventually figured it out.
DevX
+1  A: 

As Rolo says, Sessions work when for anonymous users, but When you use the auth.logout method the session is completely wiped. In your logout view, you could call auth.logout, then add whatever data you wish to persist back into the session.

Alasdair
I never thought of transferring information in the login and logout views. Great idea!
DevX