Skip to main content

Additional functionality using the GeoIP2 database.

Project description

https://badge.fury.io/py/django-geoip2-extras.svg https://travis-ci.org/yunojuno/django-geoip2-extras.svg

The master branch of this project is now Python 3.7+ and Django 2.2+ only. Legacy Python and Django versions are tagged.

Django GeoIP2 Extras

Useful extras based on the django.contrib.gis.geoip2 module, using the MaxMind GeoIP2 Lite database.

The first feature in this package is a Django middleware class that can be used to add city, country level information to inbound requests.

Requirements

This package wraps the existing Django functionality, and as a result relies on the same underlying requirements:

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!). Grab the GeoLite2-Country.mmdb.gz and GeoLite2-City.mmdb.gz files and unzip them in a directory corresponding to the GEOIP_PATH setting.

In addition, the middleware follows the ‘new’ middleware pattern, and therefore does not support Django 1.9 or below. This is a 1.10 and above package.

Installation

This package can be installed from PyPI as django-geoip2-extras:

$ pip install django-geoip2-extras

If you want to add the country-level information to incoming requests, add the middleware to your project settings. NB The GeoIP2Middleware relies on the SessionMiddleware, and must come after it:

MIDDLEWARE = (
    ...,
    'django.contrib.sessions.middleware.SessionMiddleware',
    'geoip2_extras.middleware.GeoIP2Middleware',
    ...
)

The middleware will not be active unless you add a setting for the default GEOIP_PATH - this is the default Django GeoIP2 behaviour:

# settings.py
GEOIP_PATH = os.path.dirname(__file__)

NB Loading this package does not install the MaxMind database. That is your responsibility. The Country database is 2.7MB, and could be added to most project comfortably, but it is updated regularly, and keeping that up-to-date is out of scope for this project. The City database is 27MB, and is probably not suitable for adding to source control. There are various solutions out on the web for pulling in the City database as part of a CD process.

Usage

Once the middleware is added, you will be able to access City and / or Country level information on the request object:

>>> request.geo_data.ip_address
'1.2.3.4'
>>> request.geo_data.city
'Beverley Hills'
>>> request.geo_data.postal_code
'90210'
>>> request.geo_data.region
'California'
>>> request.geo_data.country_code
'US'
>>> request.geo_data.country_name
'United States'
>>> request.geo_data.latitude
'34.0736'
>>> request.geo_data.longitude
'118.4004'

Missing / incomplete data will be None.

If the IP address cannot be found (e.g. ‘127.0.0.1’), then a default ‘unknown’ country is used, with a code of ‘XX’:

>>> geo.ip_address
'127.0.0.1'
>>> geo.country_code
'XX'
>>> geo.country_name
'unknown'
>>> geo.is_unknown
True

This prevents the middleware from re-requesting the address on each request - it effectively marks the IP as a bad address.

Tests

The project tests are run through tox.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-geoip2-extras-1.2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

django_geoip2_extras-1.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file django-geoip2-extras-1.2.tar.gz.

File metadata

  • Download URL: django-geoip2-extras-1.2.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.8.1 Darwin/19.2.0

File hashes

Hashes for django-geoip2-extras-1.2.tar.gz
Algorithm Hash digest
SHA256 4896ce385f6f1ce3930c4842114fabb01b6f14773d5f085792780f777c42a39a
MD5 709a6788b363291a22c11b7447043c69
BLAKE2b-256 be1900f398bd803d6b8ad49ea74aa6d56804ec7eda13657e70261e905fa3d320

See more details on using hashes here.

File details

Details for the file django_geoip2_extras-1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_geoip2_extras-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a363898d3245f0bc94f6f21766659ffcc5c3b5ff78b1cccff23ed57434bfdaa7
MD5 b6aceecf65eadbc4ddc357f09f986040
BLAKE2b-256 922bd017cf4b91439f552394b65fe11685dce2bc69ee125b8b12bab9fc992893

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page