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.
您在示例中使用的IP地址是本地IP地址,您不能在网络外使用它,您是否尝试使用真实的公共IP地址?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.