如何重新使用原始的admin login()和AuthenticationForm为在登录页面选中“记住我”选项的用户设置更长的cookie长度?我目前正在通过urls.py
使用内置登录url(r'^login/$','django.contrib.auth.views.login', {'template_name': 'authentication/login.html'}, name='login'),
该复选框在我的login.html中实现为:
<label><input name="remember_me" type="checkbox">Keep me logged in</label>
但我不知道如何通过AuthenticationForm将该信息传递给django.contrib.auth.views.login
目前,如果用户未选中“记住我”框,则会在settings.py中定义Cookie年龄
SESSION_COOKIE_AGE = 360
我发现了几个类似的问题,但我认为这不应该要求安装单独的应用程序。下面的代码片段(http://djangosnippets.org/snippets/1881/)似乎很有希望,但我只编写了python和Django几个月而且我无法让它工作:
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)
答案 0 :(得分:16)
django会话cookie年龄在seconds中定义。
SESSION_COOKIE_AGE = 360
表示会话将在6分钟后过期。我最近实施了“记住我&#39;功能,我设置以下内容:
SESSION_COOKIE_AGE = 60 * 60 * 24 * 30 # One month
登录视图需要覆盖,因为您已在代码段中显示。
但听起来你有一个奇怪的问题,关闭浏览器(当记住我未经检查时)并不要求用户重新登录,如果你使用set_expiry(0)则不应该发生这种情况。当你使用set_expiry(0)时,django会设置一个&#39; session&#39;长度cookie而不是固定长度的cookie,按设计它将在浏览器关闭后到期。
还有其他设置会影响浏览器关闭时清除Cookie。也许您可以尝试更改SESSION_EXPIRE_AT_BROWSER_CLOSE设置的值或检查配置中的现有值。 https://docs.djangoproject.com/en/1.10/topics/http/sessions/#browser-length-sessions-vs-persistent-sessions
答案 1 :(得分:0)
所以request.set_expiry(O)只登录一个匿名但已经过authedicated的用户.. 如果我是你,我会这样做
if request.user.is_authenticated() and request.user.id is not None:
return redirect('home')
答案 2 :(得分:-1)
from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import authenticate
from site_user.models import User
def home(request):
if request.session.has_key('username') and request.session.has_key('password'):
username = request.session['username']
password = request.session['password']
context_dict = {'username': username, 'password': password}
return render(request, 'sadmin/login.html', context=context_dict)
else:
context_dict = {'username': '', 'password': ''}
return render(request, 'sadmin/login.html', context=context_dict)
@csrf_exempt
def login(request):
if request.method == "POST":
if request.POST['is_remember_check'] == 'true':
request.session['username'] = request.POST['username']
request.session['password'] = request.POST['password']
user = authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
return JsonResponse({'result': request.POST, 'status': True})
else:
return JsonResponse({'result': request.POST, 'status': False})
function login(){
remember_checkbox_value = document.getElementsByName('remember')[0].checked;
username = document.getElementsByName('username')[0].value;
password = document.getElementsByName('password')[0].value;
var post_data = {username:username, password:password, is_remember_check:remember_checkbox_value};
$.ajax({
url: '/sadmin/login/',
method: 'POST',
data: post_data,
dataType: 'json',
success: function (response) {
if (response.status){
alert("User login is successful");
window.location.reload();
}
else{
alert("User login is not successful");
window.location.reload();
}
}
});
}
<div class="form-actions">
<label class="checkbox">
<input type="checkbox" name="remember"/> Remember me </label>
<button type="button" class="btn green-haze pull-right" onclick="login()">Login <i class="m-icon-swapright m-icon-white"></i>
</button>
</div>