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.
Installing
python -m pip install django-ip-geolocation
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.geolocation
...
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.geolocation
...
def other_view(request):
location = request.geolocation
...
Cookie:
Geolocation data stored in the Response cookie lacks the raw_data
and is base64 encoded.
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_EXTRA_PARAMS': {},
'BACKEND_USERNAME': '',
'RESPONSE_HEADER': 'X-IP-Geolocation',
'ENABLE_REQUEST_HOOK': True,
'ENABLE_RESPONSE_HOOK': True,
'ENABLE_COOKIE': False,
'FORCE_IP_ADDR': None,
}
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_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) |
Available Backends
django_ip_geolocation.backends.IPGeolocationAPI
: (Default) Using https://ipgeolocationapi.com/django_ip_geolocation.backends.IPStack
: (RequireBACKEND_API_KEY
) Using https://ipstack.com/documentationdjango_ip_geolocation.backends.IP2LocationCom
: (RequireBACKEND_API_KEY
, AcceptsBACKEND_EXTRA_PARAMS
) Using https://www.ip2location.com/web-service/ip2location
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()
Makes API calls and stores the API response in self._raw_data
.
_parse()
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
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-1.4.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5bed95f8e2560b668345ddacfdf31873f240bd8dbe01e1776e97242a103ac57 |
|
MD5 | 2fa290e53e53f4784b8d5618f30125ec |
|
BLAKE2b-256 | ba8a6d86916700a53c2915886abfdb4076044633da8f8a16bcea0818d4d178f9 |