2

I'm trying to setup geoip in Django to identify the source of a connection (to tailor content for different countries) but running into a problem.

First I execute:

from django.contrib.gis import geoip
geo = geoip.GeoIP('path to maxmind db')

Then geo.country('www.google.com') returns the US as you'd expect. Other popular websites also work fine.

However when I try it on my own client IP I get an empty record. For example: geo.country('127.6.89.129')

returns {'country_name': None, 'country': None}

What am I missing here? Does the maxmind database only cover popular sites so can't be used if I want to identify the source of the connection?

I'm also using the browser locale settings to identify language but unfortunately I need geo-location to tailor some of the content independently of language.

Freek Wiekmeijer
  • 4,556
  • 30
  • 37
ptav
  • 171
  • 1
  • 11

2 Answers2

4

The IP address you used in the example is a local IP address, you cannot use it outside your network, did you try with a real public IP address?

3

Your ip could be forwarded

def foo(request):  
    g = GeoIP()
    country = g.country(get_client_ip(request))
    country_code = country['country_code']


def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip
grigno
  • 3,128
  • 4
  • 35
  • 47