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
精彩评论