django-pageviews
django-pageviews copied to clipboard
Store the IP Address (if available) for the User with django-ipware
It is straightforward to log a users IP Address with django-ipware: https://github.com/un33k/django-ipware/blob/master/ipware/utils.py It supports both IPv4 and IPv6 as well as Python 3.
Install:
pip install django-ipware
In your view or middleware:
from ipware.ip import get_ip
ip = get_ip(request)
if ip is not None:
print "we have an IP address for user"
else:
print "we don't have an IP address for user"
It will make the best attempt to get the user's IP address or returns None to indicate that it could not determine the user's IP address.