开发者

Django "last_login" attribute in auth_user model

开发者 https://www.devze.com 2022-12-23 07:44 出处:网络
It looks like Django does not update last_login field in auth_user model when a visitor is authenticated by saved session.

It looks like Django does not update last_login field in auth_user model when a visitor is authenticated by saved session.

So in this case, how can I implement a similar feature like the "seen" field on every SO开发者_StackOverflow中文版 user's profile page.


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:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号