4

logout当用户注销时使用 Django ,所有会话值都会被刷新。即使用户注销,我有办法保留一些会话值吗?

4

1 回答 1

4

您可能想使用 cookie 而不是 session 来实现这一点。

# 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)

在您的登录视图中:

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

将以上内容推送到模板中即可显示:

Hello {{ remembered_username }}

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

于 2011-02-14T22:07:17.367 回答