Answer the question
In order to leave comments, you need to log in
How to get user's ip in Django project?
Good evening, I'm not doing a big project, you need to save the user's ip, the project is distributed at 127.0.0.1 and in nginx it is already redirected to the server address. Used info from Django documentation .
ip = request.META.get('REMOTE_ADDR', '') or request.META.get('HTTP_X_FORWARDED_FOR', '')
user.user_ip=ip
user.save()
Answer the question
In order to leave comments, you need to log in
In nginx, forward the client's ip:
proxy_set_header X-Real-IP $remote_addr;
or
uwsgi_param X-Real-IP $remote_addr;
In Djang get:
request.META['HTTP_X_REAL_IP']
Works without nginx:
class UpdateIPv4UserMeta(object):
def process_view(self, request, view_func, view_args, view_kwargs):
user = request.user
today_30min = timezone.now() + timezone.timedelta(minutes=30)
if user.is_authenticated():
if user.last_login > today_30min:
User.objects.filter(id=request.user.id).\
update(latest_ip=request.META['REMOTE_ADDR'])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question