简体   繁体   English

auth_user模型中的Django“last_login”属性

[英]Django “last_login” attribute in auth_user model

It looks like Django does not update last_login field in auth_user model when a visitor is authenticated by saved session. 当访问者通过已保存的会话进行身份验证时,Django似乎不会更新auth_user模型中的last_login字段。

So in this case, how can I implement a similar feature like the "seen" field on every SO user's profile page. 因此,在这种情况下,如何在每个SO用户的个人资料页面上实现类似“看到”字段的类似功能。

Supposed that you have last_seen_on and last_activity_ip fields in your custom UserProfile model, here is a simple middleware class that does what you want: 假设您在自定义UserProfile模型中有last_seen_on和last_activity_ip字段,这是一个简单的中间件类,可以执行您想要的操作:

import datetime

class LastSeen(object):

    def process_request(self, request):
        user = request.user
        if not user.is_authenticated(): return None  
        up = user.get_profile()
        up.last_seen_on = datetime.now()
        up.last_activity_ip = request.META['REMOTE_ADDR']
        up.save()
        return None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM