Django“记住我”具有内置的登录视图和身份验证表单 [英] Django "Remember Me" with built-in login view and authentication form
问题描述
如何重新使用原来的管理登录()和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屋!