Django IPware
A Django application to retrieve a user's IP address. It aims to provide the best attempt to get the client's IP while handling scenarios involving proxy servers and load balancers. `django-ipware` is a wrapper using `python-ipware` under the hood since version 6.0.0. The current version is 7.0.1, released on April 19, 2024, indicating active development and a regular release cadence with updates to align with new Django and Python versions.
Warnings
- gotcha There is no perfect out-of-the-box solution against IP Address Spoofing. `django-ipware` makes the best attempt but should be used only as a complement to other firewall and security measures.
- breaking Prior to version 6.0.0, `django-ipware` was a standalone package. From version 6.0.0 onwards, it became a wrapper around the `python-ipware` package. While the primary `get_client_ip` import path remains stable, internal workings and advanced configurations (especially for direct `python-ipware` usage) might differ.
- gotcha Incorrect IP address resolution behind proxy servers (e.g., load balancers, CDNs). Without proper configuration, `django-ipware` might return the proxy's IP address instead of the actual client's IP.
- deprecated Some integrating packages (e.g., `django-axes` versions 6+) have made `django-ipware` an optional dependency and renamed their internal settings related to IPware. This indicates a shift towards more explicit dependency management and configuration in ecosystem partners.
Install
-
pip install django-ipware
Imports
- get_client_ip
from ipware import get_client_ip
Quickstart
from django.http import HttpRequest, JsonResponse
from ipware import get_client_ip
def my_view(request: HttpRequest):
client_ip, is_routable = get_client_ip(request)
if client_ip is None:
# Unable to get the client's IP address
return JsonResponse({'error': 'Unable to determine client IP'}, status=400)
else:
# We got the client's IP address
ip_info = {
'ip_address': str(client_ip),
'is_publicly_routable': is_routable
}
return JsonResponse(ip_info)