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 kee开发者_开发问答p some of the session values even though the user logs out?
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
精彩评论