Django记住我没有工作

时间:2016-10-29 08:08:40

标签: django

我想要在关闭浏览器时关闭会话,但是如果用户选中了,请记住我,会话必须在浏览器关闭后打开,所以我做了这个,但它不起作用,它总是关闭。

我的settings.py:

SESSION_EXPIRE_AT_BROWSER_CLOSE = True

my views.py

if request.POST['rememberme']:
        settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False

1 个答案:

答案 0 :(得分:4)

<强> views.py

from django.contrib.auth import views as auth_views

def login_user(request, template_name='registration/login.html', extra_context=None):  
    response = auth_views.login(request, template_name)  
    if request.POST.has_key('remember_me'):    
        request.session.set_expiry(1209600) # 2 weeks

<强> urls.py

urlpatterns = [
    url(r'^login/$', login_user, name='auth_login'),
]