visitor IP address django 1

visitor IP address django

def visitor_ip_address(request):

    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

    if x_forwarded_for:
        return x_forwarded_for.split(',')[0]
    return request.META.get('REMOTE_ADDR')

Here is what the above code is Doing:
1. We’re using the request.META dictionary to get the HTTP_X_FORWARDED_FOR key.
2. If the key exists, we’re splitting the value on commas and returning the first item.
3. If the key doesn’t exist, we’re returning the REMOTE_ADDR key.

Similar Posts