ptav
ptav

Reputation: 181

Using django GeoIP and MaxMind database

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.

Upvotes: 2

Views: 1393

Answers (2)

grigno
grigno

Reputation: 3198

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

Upvotes: 3

Galia Ladiray Weiss
Galia Ladiray Weiss

Reputation: 452

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?

Upvotes: 4

Related Questions