Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass request object to form instantion with Class-based views

Hi I want to use a snippet with Class-based views, but instantion of this form must pass a User object ex.

theform = forms.PasswordReset(request.user,request.POST)

And here is a question, how to do this with Class-based views:

class UserChangePassword(FormView):
    form_class = PasswordReset
    template_name = 'profile/change_password.html'
    success_url = reverse_lazy('user_account')
like image 716
jst Avatar asked Jan 31 '26 07:01

jst


1 Answers

You have two options here:

Write your own get_form method.

def get_form(self, form_class):
    return form_class(self.request.user, self.request.POST)

Write your own get_form_kwargs and update form's code.

def get_form_kwargs(self):
    kwargs = super(UserChangePassword, self).get_form_kwargs()
    kwargs.update({'user': self.request.user, 'post': self.request.POST})
    return kwargs

In this case you also need to update form to work properly with kwargs pairs.

like image 97
Alexey Kachayev Avatar answered Feb 01 '26 20:02

Alexey Kachayev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!