python - Django, capturing client IP getting -
i trying capture client ip , store log in database, have tried using packages ipware still getting same error when run application
file "/users/abdul/documents/tutorial/drftutorial/urls.py", line 2, in <module> . import views file "/users/abdul/documents/tutorial/drftutorial/views.py", line 24, in <module> class usercreateview(generics.createapiview): file "/users/abdul/documents/tutorial/drftutorial/views.py", line 27, in usercreateview ip=get_client_ip(request) file "/users/abdul/documents/tutorial/drftutorial/views.py", line 17, in get_client_ip x_forwarded_for = request.meta.get('http_x_forwarded_for') attributeerror: 'module' object has no attribute 'meta'
i believe error happening because not accessing request method properly, using django-restframework , tutorial followed did not pass request object when calling view.
here code:
def get_client_ip(request): x_forwarded_for = request.meta.get('http_x_forwarded_for') if x_forwarded_for: ip = x_forwarded_for.split(',')[-1].strip() else: ip = request.meta.get('remote_addr') return ip class usercreateview(generics.createapiview): action = 'user registered' response1 = 'success' ip=get_client_ip(request) print ip if ip not none: ip="could not ip" else: print(" got ip!") print (" here!") logadd(action,response1,ip) serializer_class = userserializer
please try this:
def get_client_ip(request): if 'http_x_forwarded_for' in request.meta: x_forwarded_for = request.meta.get('http_x_forwarded_for') ip = x_forwarded_for.split(',')[0] else: ip = request.meta.get('remote_addr') return ip
Comments
Post a Comment