Closed aaaagrawal closed 10 years ago
@tiwilliam : This is what I have in my views.py -
import pygeoip
# Create your views here.
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
def get_city(request):
ip = get_client_ip(request)
gi = pygeoip.GeoIP('GeoLiteCity.dat')
geo_info_dict = gi.record_by_addr(str(ip))
return HttpResponse("Your IP address is " + str(ip) + "\n" + "Your city is " + geo_info_dict['city'])
And I have the binary version file GeoLiteCity.dat
in the same application directory in which views.py resides. But I am getting this IOerror - [Errno 2] No such file or directory: 'GeoLiteCity.dat'
. Any clue if I am doing something wrong?
I would guess your current work directory isn't the same as application root. If you want to debug, use os.getcwd()
to find out where you are or use a absolute path.
@tiwilliam : Thanks. Using absolute dynamic path worked. This issue can be closed then.
Hi, I want to use pygeoip library in one of my Django projects so as to know visitor's city and then offering him dynamic webpage based on the city. So these are my queries -