Here is a small snippet to get the client’s ip address in your python code in DJANGO. You might have tried the ‘REMOTE_ADDR’ key in the request object but it always returns 127.0.0.1. To fix this, you can use the ‘HTTP_X_FORWARDED_FOR’ variable like so:
from django.http import HttpRequest def mypage(request): client_address = request.META['HTTP_X_FORWARDED_FOR'] ....
The HTTP_X_FORWARDED_FOR variable stores the client’s ip when the user is going through a proxy or load balancer.
I ran into this issue when running my Django app on apache. All lookups of the ‘REMOTE_ADDR’ return localhost (127.0.0.1). This is probably a side affect of how the python module runs in apache.


February 12th, 2010 at 7:47 am
Was getting the same with REMOTE_ADDR, but works perfectly with HTTP_X_FORWARDED_FOR in my Django apps. Thanks for that!