How to get last login ip in django and save to a GenericIPAddressField?

bayman

I'm extending the user profile and added a last_ip field as shown below. How do I update this field whenever a user is logged in to its current IP? I am using allauth if it matters.

class UserProfile(models.Model):  
    user = models.OneToOneField(User)
    last_ip = models.GenericIPAddressField(protocol='IPv4', verbose_name="Last Login IP")
    location = models.CharField(max_length=50, blank=True)
Tiny Instance

For actually getting the user IP address you can utilise django-ipware. There are other methods but this app seems to cover as much as possible, you can check this question for the detailed information.

Once you have the USER_IP, you can create a middleware and update the last_ip for every request

# middleware.py
class LastLoginIP(object):
     def process_request(self, request):
         if request.user.is_authenticated():
            UserProfile.objects\
            .filter(user=request.user)\
            .update(last_ip=USER_IP)

# settings.py add the middleware
MIDDLEWARE_CLASSES = (
  ....
  your.middleware.LastLoginIP
)

Alternatively, if you already set up a system that only allows one concurrent login per profile (every time user switches devices, he/she has to login again) , then you can update the last_ip during the login.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get new Database changes since last login using Django and Python?

django 1.6 Why not just 'user.last_login = timezone.now()' and 'user.save(update_fields=['last_login'])'?

Laravel 5.5 : Last Login and Last Login IP are not to be updating on successful login

How to save user last login date if he was logged in with external login provider using ASP .Net Identity?

How to add the last_login_ip, when the user login if using `rest-auth`?

How to check new posts since users last login Django

Django: IPv4 only for GenericIPAddressField

How to get User session in Login handling in Django?

How to GET request if login_required in DJango

How to get the last 10 item data in django?

How to get second last record in a queryset in Django?

How to get name of last migration programmatically in Django

Wordpress - How to get device type and last login location with php

How to save and display User IP and location with django and GeoIP2

Laravel 5.4 Save last login in logs table

how to get the last digit from format like ip address javascript?

how to get the last octet of an IP address into 3 seperate int's

How to get my own IP address and save it to a variable in a shell script?

c# Core MVC user identity / how can I save last login (via cookie)

How to build login system so that only last user logged in can save?

How to get first login and last logout from multiple login and logout for one single date?

How to print last login time?

django after login return to last page

How to save last components changes?

How to save pointer to last node

Django - how get last item from queryset in template

Django- How to get second last record in a queryset?

How to get last six month data from date field - django

Custom save method in django model and login issue