Skip to main content

Django request/response hook (Middleware and Decorator) to geolocate visitors using their IP address

Project description

PyPI version Downloads Build Status

Django Ip Geolocation

Django request/response hooks to geolocate visitors by their ip address.


python -m pip install django-ip-geolocation



Use decorators to decorate views:

from django_ip_geolocation.decorators import with_ip_geolocation

def api_view(request):
   location = request.geolocation


First you need to add the middleware into your


Then the location is available to all views in request and response:

def api_view(request):
   location = request.geolocation
def other_view(request):
  location = request.geolocation


Geolocation data stored in the Response cookie lacks the raw_data and is base64 encoded.

User consent

Developers must implement a helper function to check if the user consented or not and configure it in the

By default if the developer didn't provide a validation function, we consider it as implicit consent.

Here is an example of the helper function:

def check_user_consent(request):
  if request.user.is_consented: # this is only an example.
    return True
  return False


You can configure settings for your hook in the as follow:

    'BACKEND': 'django_ip_geolocation.backends.IPGeolocationAPI',
    'BACKEND_API_KEY': '',
    'RESPONSE_HEADER': 'X-IP-Geolocation',
    'ENABLE_COOKIE': False,
    'FORCE_IP_ADDR': None,

Those are the default settings, that will be overwritten by those set in

setting description default value (type)
BACKEND Backend class used to detect the geolocation django_ip_geolocation.backends.IPGeolocationAPI (string class path)
BACKEND_API_KEY Api key or token for the backend Empty (string)
BACKEND_EXTRA_PARAMS Extra parameters specific to the backend {} (dict)
BACKEND_USERNAME username for the backend Empty (string)
RESPONSE_HEADER Custom response header to store the geolocation X-IP-Geolocation (string)
ENABLE_REQUEST_HOOK Enable or disable hook on request True (bool)
ENABLE_RESPONSE_HOOK Enable or disable hook on request True (bool)
ENABLE_COOKIE Enable or disable geolocation data in cookie False (bool)
FORCE_IP_ADDR Force ip address, rather than using visitor ip None (string)
USER_CONSENT_VALIDATOR A function path to check if the current user gave his consent None (string, function path)

Available Backends

Implementing your own backend

If you want to add a new backend, you need to inherit from django_ip_geolocation.backends.base, then you need to implement geolocate() and _parse().


Makes API calls and stores the API response in self._raw_data.


Parse raw data stored in self._raw_data and assigns values to the class attribute, such as self._continent, self._county, self._geo.

self._country is a dict with code and name keys.

self._geo is a dict with latitude and longitude keys.

Project details

Download files

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

Files for django-ip-geolocation, version 1.6.0
Filename, size File type Python version Upload date Hashes
Filename, size django-ip-geolocation-1.6.0.tar.gz (6.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page