This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hello friends! I have a custom user model in which ip_address is one of the fields. I have provided users a standard sign up process using model form and a social login option using Google for a quick login process. I have used 'django-allauth' library for integrating social logins.
If a user chooses to signup using the regular signup process, I am able to capture the IP address successfully using request.META.get('HTTP_X_FORWARDED_FOR') or request.META.get('REMOTE_ADDR') . But When a user uses the social login option (Google), since everything is being handled by django-allauth, I am not sure how I can also capture the IP Address at the time of sign in. Is there any way I can try some way via signals to achieve this? Or is there any other way?
Any guidance on this would be appreciated.
Edit: After banging my head around for a few hours, and thanks to suggestions from u/xNovax and u/oliw, I finally managed to capture the IP address of user successfully. I was previously trying to write some method inside a view. After going through multiple posts on the internet, below is a solution that finally worked for me. I created a signal to capture or update the IP Address every time a user successfully logs into the website.
signals.py file:
from django.contrib.auth import user_logged_in
def get_ip(request):
"""Function to capture IP Address of user"""
try:
x_forward = request.META.get("HTTP_X_FORWARDED_FOR")
if x_forward:
ip = x_forward.split(",")[0]
else:
ip = request.META.get("REMOTE_ADDR")
except:
ip = ""
return ip
def login_ip(sender, user, request, **kwargs):
user.ip_address = get_ip(request)user.save()
user_logged_in.connect(login_ip)
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/django/comm...