get_ip_address() not working when used Custom Decorators

56 views
Skip to first unread message

Mannu Gupta

unread,
Sep 28, 2017, 3:03:09 PM9/28/17
to Django users
While making a customer decorator in django, the code is here :-

def owner_required(function):
   
def wrap(request, *args, **kwargs):
       
print(request)
        ip_address
= get_client_ip(request)
        ip_exist
= Node.objects.get(ip_address=ip_address)
       
if ip_exist:
           
return function(request, *args, **kwargs)
       
else:
           
raise PermissionDenied
   
return wrap

my code for get_ip_address() is :-

def get_client_ip(request):
   
""" Extract ip address from a Django request object
    """

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

The error i am getting is :-

x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
AttributeError: 'function' object has no attribute 'META'


That get_client_ip() is working fine when used in normal function, but don't know why it is not working when i am using it a decorator.

What might be the problem ?

Thanks in advance for replies.

Daniel Roseman

unread,
Sep 28, 2017, 4:04:26 PM9/28/17
to Django users
Please show where you're using that decorator.
-- 
DR. 

Mannu Gupta

unread,
Sep 28, 2017, 4:36:37 PM9/28/17
to Django users
I am just using it in a view function. For example:-

@owner_required
def all(request, **kwargs):
   
pass

Constantine Covtushenko

unread,
Sep 29, 2017, 10:38:22 PM9/29/17
to django...@googlegroups.com
Hi Mannu,

It seems like all are ok.
Sorry, but do you use it in just one place?
And do you see response in console from your print?

Regards,
Constantine C.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e34ae73-5697-4a93-9402-c0ca1f4abd70%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Sincerely yours,
Constantine C

Mannu Gupta

unread,
Sep 30, 2017, 4:02:58 PM9/30/17
to Django users
Hello Constantine C,

Thanks for your reply!

What do you mean to say just one place ?
I just printed the request using `print(request)` and getting this `<function all at 0x7f6ffc65a050>` ( don't know what this actually is )

Am i using the following approach.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Constantine Covtushenko

unread,
Oct 1, 2017, 9:20:01 AM10/1/17
to django...@googlegroups.com
Sorry for late response,

I meant that you might using that decorator in 2 places.
And asked about that.

It seems like you specified `@owner_required` to decorate your view function. But that decorator behave like `@owner_required()`.

With your last response I see that decorated function instead of `request` receives `function all`. It looks like improperly used decorator.

More than that I do not have more ideas from what you provided.


To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

mitesh_django

unread,
Oct 1, 2017, 9:38:47 AM10/1/17
to Django users
Manu,

Try print kwargs.get('request')... 

You should get Django HTTPRequest Object, pass that to get_client_ip method.
Reply all
Reply to author
Forward
0 new messages