Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, Retrieve IP location

I would like to redirect my users to specific location areas in my website, by detecting their location from their IP address.

What would be the best way to achieve this under Django 1.1.1 ?

Thanks

Edit: I want city based locationing on europe.

like image 352
Hellnar Avatar asked Feb 07 '10 19:02

Hellnar


People also ask

How do you track IP address in Django?

The first thing we have to do is start a project and start an app inside the project and follow the steps given below. Steps to get the IP address of the user: Step 1- Define two functions in views.py which will store the IP address and show it as an output. Step 2- Changes in urls.py to call the function.

What library do you need to work with geolocation in Django and Python?

In order to perform IP-based geolocation, the GeoIP2 object requires the geoip2 Python library and the GeoIP Country and/or City datasets in binary format (the CSV files will not work!).

How do you check geolocation?

Test website Geolocation using VPN A virtual private network (VPN) proxy server can make it look like the system is in a different location than its actual one. This is a good thing as far as our use case of geolocation testing is concerned. You open a VPN, change location to a specific country and start testing.


1 Answers

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.

like image 143
Nick Presta Avatar answered Oct 22 '22 06:10

Nick Presta