Django, Retrieve IP location

GeoDjango looks like it will suit your needs. I’m not sure exactly how you would want to direct users, but using the GeoIP API, you can do something like:

from django.contrib.gis.utils import GeoIP
g = GeoIP()
ip = request.META.get('REMOTE_ADDR', None)
if ip:
    city = g.city(ip)['city']
else:
    city = 'Rome' # default city

# proceed with city

The Docs explain things in great detail; I would take a moment to read through them thoroughly.

Leave a Comment