Skip to content

Instantly share code, notes, and snippets.

@renatooliveira
Last active December 19, 2015 01:09
Show Gist options
  • Select an option

  • Save renatooliveira/5873721 to your computer and use it in GitHub Desktop.

Select an option

Save renatooliveira/5873721 to your computer and use it in GitHub Desktop.

Para instalar o django-ipware

pip install django-ipware

 >>> g.city('74.125.79.147')
 {'city': 'Ackworth',
 'region': 'IA',
 'area_code': 970,
 'longitude': -93.434700012207031,
 'country_code3': 'USA',
 'latitude': 41.363498687744141,
 'postal_code': '50001',
 'dma_code': 679,
 'country_code': 'US',
 'country_name': 'United States'}
from ipware.ip import get_ip_address_from_request
from django.contrib.gis.utils import GeoIP
def index(request):
ip = get_ip_address_from_request(request)
g = GeoIP()
cidade = g.city(ip)['city']
return render(request, 'index.html', {'cidade': cidade})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment