Geolocation with GeoIP2
For Django Lovers,
At times you might want to record a user's specific location from their IP Address, for informational and security purposes.
For example, when displaying to a user, information about where they are currently logged in, or where all logged-in devices on their account are located, so they can either log out of all other sessions, or, be informed of an unfamiliar login.
This is how you can setup GEOIP in your django application:
-
Install GEOIP2 in your environment:
$ pip install geoip2
- Signup on maxmind dot com, and download the GEOIP2 Binary(.mmdb) format. You can choose to download GeoLite2City.mmdb, or GeoLite2Country.mmdb . It comes as a zip file, so you would have to download the zip file and extract it.
Note that, as Django has it in documentation, you can also get it from DB-IP dot com.
- Put the extracted folder in a folder in your django application and reference to it.
I recommend that you put it in the root folder.
Say, you name the extracted folder "geoiptwo", and your root directory is defined as: root_dir = Path(), the structure will look like this in your django project:
django_project/
|-----.github/
|-----.gitignore
|-----.readthedocs.yml
|-----README.md
|-----geoiptwo/
└── GeoLite2-____.mmdb
|-----app_name/
└──admin.py
└──models.py
└──views.py
└──e.t.c
|-----settings_folder/
└──settings.py
and the link to reference it will be either:
a. path_to_root_folder/geoiptwo/GeoLite2City.mmdb
or
b. path_to_root_folder/geoiptwo/GeoLite2Country.mmdb
4. In your settings.py, add GEOIP_PATH. This is the full path to where the city or country data files (.mmdb) are located. That is,:
from pathlib import Path
root_dir = Path(how_you_access_path_here)
GEOIP_PATH = str(root_dir / "geoiptwo/GeoLite2-City.mmdb")
or
GEOIP_PATH = str(root_dir / "geoiptwo/GeoLite2-Country.mmdb")
I recommend that, you do not change the names of the 'geoLite2__.mmdb' data files, as they are default values of GEOIP_CITY and GEOIP_COUNTRY tags.
Otherwise, you have to explicitly set the keywords, i.e.,
GEOIP_CITY = "your_new_file_name.mmdb"
or
GEOIP_COUNTRY = "your_new_file_name.mmdb"
- To apply in your project, reference pypi . org / project / geoip2 for how to use. However, below is an example of how to use the City database:
access interactive shell:
$ python manage.py shell
>>> from geoip2.database import Reader
>>> import settings # from your setttings directory
>>> # This creates a Reader object. You should use the same object across multiple requests as creation of it is expensive.
>>> with Reader(settings.GEOIP_PATH) as reader:
>>> # Replace "city" with the method corresponding to the database that you are using, e.g., "country".
>>> response = reader.city('203.0.113.0')
# try:
>>> print(response.country.iso_code) // 'US'
>>> print(response.country.name) // 'United States'
>>> print( response.city.name) // 'Minneapolis'
So by following this guide, you should be able to record geographical information of your users via your django application, to effectively assist them with knowing who else has access to their account.