Package: django-ip-geolocation
Django Ip GeolocationDjango request/response hooks to geolocate visitors by their ip address.Installingpython -m pip install django-ip-geolocationUsageDecoratorUse decorators to decorate views:fromdjango_ip_geolocation.decoratorsimportwith_ip_geolocation@with_ip_geolocationdefapi_view(request):location=request.geolocation...MiddlewareFirst you need to add the middleware into yoursettings.py.MIDDLEWARE=[...'django_ip_geolocation.middleware.IpGeolocationMiddleware',...]Then the location is available to all views in request and response:defapi_view(request):location=request.geolocation...defother_view(request):location=request.geolocation...CookieGeolocation data stored in the Response cookie lacks theraw_dataand is base64 encoded.User consentDevelopers must implement a helper function to check if the user consented or not and configure it in thesettings.py.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:defcheck_user_consent(request):ifrequest.user.is_consented:# this is only an example.returnTruereturnFalseSettingsYou can configure settings for your hook in thesettings.pyas 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,'USER_CONSENT_VALIDATOR':None}Those are the default settings, that will be overwritten by those set insettings.pysettingdescriptiondefault value (type)BACKENDBackend class used to detect the geolocationdjango_ip_geolocation.backends.IPGeolocationAPI(string class path)BACKEND_API_KEYApi key or token for the backendEmpty (string)BACKEND_EXTRA_PARAMSExtra parameters specific to the backend{}(dict)BACKEND_USERNAMEusername for the backendEmpty (string)RESPONSE_HEADERCustom response header to store the geolocationX-IP-Geolocation(string)ENABLE_REQUEST_HOOKEnable or disable hook on requestTrue(bool)ENABLE_RESPONSE_HOOKEnable or disable hook on requestTrue(bool)ENABLE_COOKIEEnable or disable geolocation data in cookieFalse(bool)FORCE_IP_ADDRForce ip address, rather than using visitor ipNone(string)USER_CONSENT_VALIDATORA function path to check if the current user gave his consentNone(string, function path)Available Backendsdjango_ip_geolocation.backends.IPGeolocationAPI: (Default) Usinghttps://ipgeolocationapi.com/django_ip_geolocation.backends.IPStack: (RequireBACKEND_API_KEY) Usinghttps://ipstack.com/documentationdjango_ip_geolocation.backends.IP2LocationCom: (RequireBACKEND_API_KEY, AcceptsBACKEND_EXTRA_PARAMS) Usinghttps://www.ip2location.com/web-service/ip2locationdjango_ip_geolocation.backends.IPDataCo: (RequireBACKEND_API_KEY) Usinghttps://docs.ipdata.co/Implementing your own backendIf you want to add a new backend, you need to inherit fromdjango_ip_geolocation.backends.base, then you need to implementgeolocate()and_parse().geolocate()Makes API calls and stores the API response inself._raw_data._parse()Parse raw data stored inself._raw_dataand assigns values to the class attribute, such asself._continent,self._county,self._geo.self._countryis a dict withcodeandnamekeys.self._geois a dict withlatitudeandlongitudekeys.