PasswordChangeForm with custom user model
I recently implemented my own user model by subclassing an abstract user.
class NewUserModel(AbstractUser):
After that, it PasswordChangeForm
stopped working. I fixed the problem in UserCreationForm
by overriding the model field class Meta:
. However ChangePasswordForm
does not specify the model and I see no reason why it shouldn't work with a new custom model.
views.py
class PasswordChangeView(LoginRequiredMixin, FormView):
template_name = 'change_password.html'
form_class = PasswordChangeForm
def get_form_kwargs(self):
kwargs = super(PasswordChangeView, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
source to share
Just spent most of the day trying to achieve this. Eventually I found out that it was quite easy to implement it with FBV:
@login_required
def UpdatePassword(request):
form = PasswordChangeForm(user=request.user)
if request.method == 'POST':
form = PasswordChangeForm(user=request.user, data=request.POST)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
return render(request, 'user/password.html', {
'form': form,
})
source to share