Django "last_login" attribute in auth_user model

It seems that Django does not update the last_login field in the auth_user model when the visitor authenticates with the saved session.

So, in this case, how can I implement a similar function, such as a “visible” field on each page of the SO user profile.

+5
source share
1 answer

Suppose you have the last_seen_on and last_activity_ip fields in your UserProfile user 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
+10
source

All Articles