I have the user registration form made in django.
I want to know the city from which the user is registering.
Is there any way that i get the IP address of the user and then somehow get the city for that IP. using some API or something
I have the user registration form made in django.
I want to know the city from which the user is registering.
Is there any way that i get the IP address of the user and then somehow get the city for that IP. using some API or something
This is definetly not a good idea to geolocate the user ip, this is intrusive and will be unexact 80% of times..
The only way if your apps really need geodatas is to ask the user if he want to allow it:
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
REMOTE_ADDR property of request.META. Check this post for a complete solution for remote address.
While registering, you can do:
addr = request.META.get('REMOTE_ADDR', None)
and save this IP address.
Now, you can get the city using GeoIP like this:
from django.contrib.gis.utils import GeoIP
g = GeoIP()
addr = request.META.get('REMOTE_ADDR', None) #Or retrive this from the userprofile you saved while registering
city = 'New York' #Or any default city
if addr:
city = g.city(addr)['city']
Here's your GeoIP functionality: https://github.com/aschem/django-user-location
Not in any reliable way, or at least not in Django. The problem is that user IPs are usually dynamic, hence the address is changing every couple of days. Also some ISPs soon will start to use a single IP for big blocks of users (forgot what this is called) since they are running out of IPv4 IP addresses... In other words, all users from that ISP within a whole state or even country will have a single IP address.
So using the IP is not reliable. You could probably figure out the country or region of the user with reasonable accuracy however my recommendation is not to use the IP for anything except logging and permission purposes (e.g. blocking a spam IP).
If you want user locations, you can however use HTML5 location API which will have a much better shot of getting more accurate location since it can utilize other methods such us using a GPS sensor in a phone.