django usermodel django.contrib.auth.models.User有一个字段'last_login'记录用户成功登录的时间。
但是我没有在函数from django.contrib.auth import login
或from django.contrib.auth import authenticate
中看到诸如'last_login = datetime.now()'之类的代码。我还检查了django.contrib.auth.signals.user_logged_in
。
更新字段'last_login'的代码在哪里?
以下是所有相关的来源,登录过程如何调用update_last_login?我在登录视图或Authenticate函数suoure代码中看不到任何代码。
def update_last_login(sender, user, **kwargs):
"""
A signal receiver which updates the last_login date for
the user logging in.
"""
user.last_login = timezone.now()
user.save(update_fields=['last_login'])
user_logged_in.connect(update_last_login)
from django.dispatch import Signal
user_logged_in = Signal(providing_args=['request', 'user'])
@sensitive_post_parameters()
@csrf_protect
@never_cache
def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
current_app=None, extra_context=None):
"""
Displays the login form and handles the login action.
"""
redirect_to = request.REQUEST.get(redirect_field_name, '')
if request.method == "POST":
form = authentication_form(request, data=request.POST)
if form.is_valid():
# Ensure the user-originating redirection url is safe.
if not is_safe_url(url=redirect_to, host=request.get_host()):
redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
# Okay, security check complete. Log the user in.
auth_login(request, form.get_user())
return HttpResponseRedirect(redirect_to)
else:
form = authentication_form(request)
current_site = get_current_site(request)
context = {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
class ModelBackend(object):
"""
Authenticates against settings.AUTH_USER_MODEL.
"""
def authenticate(self, username=None, password=None, **kwargs):
UserModel = get_user_model()
if username is None:
username = kwargs.get(UserModel.USERNAME_FIELD)
try:
user = UserModel._default_manager.get_by_natural_key(username)
if user.check_password(password):
return user
except UserModel.DoesNotExist:
# Run the default password hasher once to reduce the timing
# difference between an existing and a non-existing user (#20760).
UserModel().set_password(password)
答案 0 :(得分:5)
user_logged_in
信号连接到django.contrib.auth.models.update_last_login
函数,它生成:
user.last_login = timezone.now()
user.save(update_fields=['last_login'])
答案 1 :(得分:0)
我认为做这件事的最好方法是
request_user, data = requests.get_parameters(request)
user = requests.get_user_by_username(data['username'])
update_last_login(None, user)
您还可以通过执行以下操作来获取请求对象的用户。
user = request.user
答案 2 :(得分:0)
在djnago 1.11上 添加此类:
class IsAuthenticated(BasePermission):
"""
Allows access only to authenticated users.
"""
def has_permission(self, request, view):
if request.user and request.user.is_authenticated:
user = request.user
user.last_login = timezone.now()
user.save(update_fields=['last_login'])
return request.user and request.user.is_authenticated
更改设置文件:
'DEFAULT_PERMISSION_CLASSES': (
# custom class
'IsAuthenticated',
),