Skip to main content

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

Project description

Django Ip Geolocation:

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

Usage:

Decorator:

Use decorators to decorate views :

from django_ip_geolocation.decorators import with_ip_geolocation

@with_ip_geolocation
def api_view(request):
   location = request.location
   ...

Middleware:

First you need to add the middleware into your settings.py

MIDDLEWARE = [
    ...
    'django_ip_geolocation.middleware.IpGeolocationMiddleware',
    ...
]

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

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

Settings

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

IP_GEOLOCATION_SETTINGS = {
    'BACKEND': 'django_ip_geolocation.backends.IPGeolocationAPI',
    'BACKEND_API_KEY': '',
    'BACKEND_USERNAME': '',
    'RESPONSE_HEADER': 'X-IP-Geolocation',
    'ENABLE_REQUEST_HOOK': True,
    'ENABLE_RESPONSE_HOOK': True,
}

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

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_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)

Available Backends:

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-ip-geolocation-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

File details

Details for the file django-ip-geolocation-0.1.0.tar.gz.

File metadata

  • Download URL: django-ip-geolocation-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.6

File hashes

Hashes for django-ip-geolocation-0.1.0.tar.gz
Algorithm Hash digest
SHA256 deafc085a690fb3def01bc8fb43dee3d04af738a434ad7a3cb6d7b4aaa43da09
MD5 953d4e42b65f18ade1b0c5a37bf417c0
BLAKE2b-256 fa6d75cfa38b849918c8836f8d3c51a8c562348995c1b18816ac5bfb3c51a3e8

See more details on using hashes here.

Supported by

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