开发者

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.

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜