Current user ip address on form.py

26 views
Skip to first unread message

lakshitha kumara

unread,
Apr 27, 2018, 8:28:13 AM4/27/18
to Django users
Hello Guys

Is there way to get current user ip address on form.py 

Thanks

Andréas Kühne

unread,
Apr 27, 2018, 8:36:36 AM4/27/18
to django...@googlegroups.com
Hi,

You can get the IP address from the request. See here:

Regards,

Andréas

--
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/d7edf4df-90c5-4b48-afd5-a31fd157d2f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

lakshitha kumara

unread,
Apr 27, 2018, 8:45:05 AM4/27/18
to Django users
Hello Andréas

Thank for your reply. but unfortunately i need get user ip on form.py or custom py file in django. is there way to to that ? 
 
Some one post on this example can be work but its also doesn't work 

class RegistrationForm(UserCreationForm):

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request')
        super(RegistrationForm, self).__init__(*args, **kwargs)

On Friday, April 27, 2018 at 6:06:36 PM UTC+5:30, Andréas Kühne wrote:
Hi,

You can get the IP address from the request. See here:

Regards,

Andréas

2018-04-27 14:28 GMT+02:00 lakshitha kumara <laksith...@gmail.com>:
Hello Guys

Is there way to get current user ip address on form.py 

Thanks

--
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...@googlegroups.com.

James Farris

unread,
Apr 27, 2018, 10:08:38 AM4/27/18
to django...@googlegroups.com

Andréas Kühne

unread,
Apr 27, 2018, 10:59:44 AM4/27/18
to django...@googlegroups.com
If you want the request in a form, you need to supply it.

You can use the code you posted - but then when you create the form - you need to add the request.

Something like: 

form = Form(request=request) 

every time you init the form - then it will work for you.

Regards,

Andréas

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

--
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.
Reply all
Reply to author
Forward
0 new messages