avatar
avatar

Reputation: 12495

django - storing session values after user logout

Using the Django logout when the user is logging out all the sessions values get flushed. I there a way to keep some of the session values even though the user logs out?

Upvotes: 4

Views: 2531

Answers (1)

Thierry Lam
Thierry Lam

Reputation: 46264

You might want to use cookie instead of session to achieve this.

# views.py, login view
# After you have authenticated a user
username = 'john.smith'  # Grab this from the login form

# If you want the cookie to last even if the user closes his browser,
# set max_age to a very large value, otherwise don't use max_age.
response = render_to_response(...)
response.set_cookie('the_current_user', username, max_age=9999999999)

In your login view:

remembered_username = request.COOKIES.get('the_current_user', '')

Push the above to the template to display:

Hello {{ remembered_username }}

Reference: http://docs.djangoproject.com/en/1.2/ref/request-response/#django.http.HttpResponse.set_cookie

Upvotes: 4

Related Questions