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.
Common errors
-
ModuleNotFoundError: No module named 'ipware'
cause The 'django-ipware' package has not been installed in the active Python environment or is not accessible.fixInstall the package using pip: `pip install django-ipware` -
client_ip is None (from get_client_ip) OR ip_address is None OR returns '127.0.0.1' (or local IP)
cause The library is unable to reliably determine the client's public IP address, often because the application is behind proxy servers or load balancers, and `django-ipware` is not configured to correctly interpret the relevant HTTP headers. This can also occur when testing in a local development environment.fixConfigure `proxy_count`, `proxy_trusted_ips`, or `request_header_order` in the `get_client_ip` function to instruct `ipware` how to correctly parse IP addresses from proxy headers. For example: `client_ip, is_routable = get_client_ip(request, proxy_count=1, request_header_order=('X_FORWARDED_FOR', 'REMOTE_ADDR'))` -
from django_ipware import get_client_ip
cause Developers often mistakenly attempt to import `get_client_ip` from `django_ipware` (the package name) instead of the correct top-level module `ipware`.fixThe correct import statement is `from ipware import get_client_ip`
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 django_ipware import 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)