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:
django_ip_geolocation.backends.IPGeolocationAPI
: (Default) Using https://ipgeolocationapi.com/django_ip_geolocation.backends.IPStack
: (RequireBACKEND_API_KEY
) Using https://ipstack.com/documentation
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()
.
geolocate()
:
This method will make the external api call. It should also store the api response in self._raw_data
. and call self._parse()
.
_parse()
:
This method will parse raw data stored in self._raw_data
and assign 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-ip-geolocation-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ef6d0638de136978b49089d3afdaacf84362443cce44424770351cb3675c0d2 |
|
MD5 | 5f627dcfec79a77ff32361c35b5efe48 |
|
BLAKE2b-256 | 80d7fc92944cf5224602d663d92b4a929b0ff1f0968ebd8d2fb52bdd51c4a7cc |