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

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 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.

Usage

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

>>> request.country
{
    'ip_address': '1.2.3.4',
    'country_code': 'GB',
    'country_name': 'United Kingdom'
}

If the IP address cannot be found (e.g. localhost), then a default ‘unknown’ country dict is used.

>>> request.country
{
    'ip_address': '127.0.0.1',
    'country_code': 'XX',
    'country_name': 'unknown'
}

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-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_geoip2_extras-0.1.2-py2-none-any.whl (8.1 kB view details)

Uploaded Python 2

File details

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

File metadata

File hashes

Hashes for django-geoip2-extras-0.1.2.tar.gz
Algorithm Hash digest
SHA256 83e64a6376cf591d138bd348185d3189e5ed190f356f14a68b734caa76291c0a
MD5 19261b166dd205c6b032f446bdaa2cea
BLAKE2b-256 65a7335be3aa0429b36c46ef1bcedfd3be67d72451029c1e28b0b77aee26789e

See more details on using hashes here.

File details

Details for the file django_geoip2_extras-0.1.2-py2-none-any.whl.

File metadata

File hashes

Hashes for django_geoip2_extras-0.1.2-py2-none-any.whl
Algorithm Hash digest
SHA256 e52009dc24180da4efebff75b911f23b3e2b3fbbc5f32afcf6f3459bf1a3fa0b
MD5 7ed578fc51ab16c496f63a3dcf38a78d
BLAKE2b-256 9394a5be5970d74c64a852096b8fc76a13e3bbe7dbb1bcab920abe73429805a5

See more details on using hashes here.

Supported by

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