Django IPware

7.0.1 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

A simple Django view demonstrating how to retrieve the client's IP address using `get_client_ip`. It returns the IP address and a boolean indicating if it's publicly routable. Remember to pass the `request` object.

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)

view raw JSON →