1👍
✅
You can do this with some middleware and the GIS contrib app, which provides a wrapper for geoip lookups.
Creating a simple table to log your extra details, and then use something like the following:
from django.contrib.gis.utils import GeoIP
from logger.models import Log # your simple Log model
def get_ip(request):
xff = request.META.get('HTTP_X_FORWARDED_FOR')
if xff:
return xff.split(',')[0]
return request.META.get('REMOTE_ADDR')
class UserLocationLoggerMiddleware(object):
def process_request(self, request):
if request.user and request.user.is_superuser:
# Only log requests for superusers,
# you can control this by adding a setting
# to track other user types
ip = get_ip(request)
g = GeoIP()
lat,long = g.lat_lon(ip)
Log.objects.create(request.user, ip, lat, long)
Consult the middleware documentation on how to setup and configure custom middleware.
Source:stackexchange.com