[Django]-Django Forms clean() method – need IP address of client

13👍

So give yourself a reference to it.

class MyModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(MyModelForm, self).__init__(*args, **kwargs)


    def clean(self):
        ip_address = self.request['META']['REMOTE_ADDR']

and in your view:

myform = MyModelForm(request.POST, request=request)

Leave a comment