Django“记住我”具有内置的登录视图和身份验证格式 [英] Django "Remember Me" with built-in login view and authentication form

查看:327
本文介绍了Django“记住我”具有内置的登录视图和身份验证格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在登录页面上检查记住我选项的用户,如何重用原始的admin login()和AuthenticationForm来设置较长的cookie长度?我目前正在使用内置的登录通过urls.py

How can I reuse the original admin login() and AuthenticationForm to set longer cookie length for users with "remember me" option checked at login page? I am currently using the built-in login through urls.py

url(r'^login/$','django.contrib.auth.views.login', {'template_name': 'authentication/login.html'}, name='login'),

该复选框在我的login.html中实现为:

The checkbox is implemented in my login.html as:

<label><input name="remember_me" type="checkbox">Keep me logged in</label>

但我不知道如何将该信息通过AuthenticationForm传递给django.contrib.auth。 views.login

but I am not sure how to pass that information through the AuthenticationForm to the django.contrib.auth.views.login

目前,如果用户记录记住我框未选中,cookie年龄在settings.py

Currently, if the user logs "remember me" box unchecked, the cookie age is defined in settings.py

SESSION_COOKIE_AGE = 360

类似的问题,但我不认为这应该需要一个单独的应用程序来安装。以下代码段( http://djangosnippets.org/snippets/1881/ )似乎很有前途,但我有编码的python和Django只有几个月,我无法让它工作:

I found couple of similar questions but I don't think this should require a separate app to be installed. The below snippet (http://djangosnippets.org/snippets/1881/) seemed promising but I have coded python and Django only for couple of months and I wasn't able to get it working:

def login(request, *args, **kwargs):
    if request.method == 'POST':
        if not request.POST.get('remember_me', None):
            request.session.set_expiry(0)
    return auth_views.login(request, *args, **kwargs)


推荐答案

django会话cookie年龄在

The django session cookie age is defined in seconds.

SESSION_COOKIE_AGE = 360

表示会话将在6分钟后过期。我最近实施了记住我功能,并设置了以下内容:

means that the session will expire after 6 minutes. I've recently implemented the 'Remember Me' feature and I set the following:

SESSION_COOKIE_AGE = 60 * 60 * 24 * 30 # One month

登录视图需要覆盖,如您在代码段中所示。

The login view needs override as you've shown in the snippet.

但是听起来像是有一个奇怪的问题,关闭浏览器(当记住我未被选中时)不要求用户重新登录,如果您使用set_expiry(不需要) 0)。当您使用set_expiry(0)时,django将设置一个会话长度的cookie,而不是固定长度的cookie,并且按照设计,它会在浏览器关闭后过期。

But sounds like you're having an odd issue where closing the browser (when remember me is unchecked) is not requiring the user to re-login which should not happen if you use set_expiry(0). When you use set_expiry(0), the django sets a 'session' length cookie as opposed to a fixed length cookie and by design it would expire after browser close.

影响浏览器关闭清除Cookie的其他设置。也许您可以尝试更改SESSION_EXPIRE_AT_BROWSER_CLOSE设置的值,或者检查其配置中的现有值。 https:// docs。 djangoproject.com/en/1.10/topics/http/sessions/#browser-length-sessions-vs-persistent-sessions

There's another settings that affects clearing cookie on browser close. Maybe you can try altering the SESSION_EXPIRE_AT_BROWSER_CLOSE setting's value or check it's existing value in your configuration. https://docs.djangoproject.com/en/1.10/topics/http/sessions/#browser-length-sessions-vs-persistent-sessions

这篇关于Django“记住我”具有内置的登录视图和身份验证格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆