Django 1.9 and above
You need to get the GeoIp2-database download from the MAXMIND official website. You can download free files by registering as a user.
# cmd
pip install django-geoip2-extras
# setting.py
MIDDLEWARE = [
~
'django.contrib.sessions.middleware.SessionMiddleware',
#Add GeoIP2 Middleware after Session Middleware
'geoip2_extras.middleware.GeoIP2Middleware',
~
]
# setting.py
GEOIP_PATH = os.path.join('The path where mmdb is located')
#Import geoip2
from django.contrib.gis import geoip2
#Instantiation
geo_ip2 = geoip2.GeoIP2()
#Set the domain name or IP address in the argument
geo_ip2.city(query)
geo_ip2.country(query)
geo_ip2.country_code(query)
geo_ip2.country_name(query)
django-geoip2-extras 1.2(PyPI) GeoIP2 (Django official documentation)
Recommended Posts