Accessing Request Object In Form

Posted: May 18th, 2010 | Author: Arif Harbott | Filed under: Django | Tags: , , | No Comments »

Something that I use quite regularly is accessing the request object in a form.

It is actually pretty simple, in your form you add a custom __init__ function that sets the request as a parameter:

class PasswordRecoverForm(PasswordResetForm):
    def __init__(self, *args, **kwargs):
    self.request = kwargs.pop('request', None)
    super(PasswordRecoverForm, self).__init__(*args, **kwargs)
def clean_email(self):
    """Do something with the request"""
    print self.request

Then in your view code you can pass the request as a parameter.

form = PasswordRecoverForm(request=request)