Reputation: 3060
I recently implemented my own user model by subclassing abstract user.
class NewUserModel(AbstractUser):
After I did this the PasswordChangeForm
stopped working. I fixed the issue in the UserCreationForm
by overriding the class Meta:
model field. However, the ChangePasswordForm
doesn't specify a model and I can't see any reason why it shouldn't work with the new user 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
Upvotes: 8
Views: 6988
Reputation: 124
CBV version:
class PasswordChangeView(LoginRequiredMixin, FormView): model = CustomUser form_class = PasswordChangeForm template_name = 'password_change.html' def get_form_kwargs(self): kwargs = super(PasswordChangeView, self).get_form_kwargs() kwargs['user'] = self.request.user if self.request.method == 'POST': kwargs['data'] = self.request.POST return kwargs def form_valid(self, form): form.save() update_session_auth_hash(self.request, form.user) return super(PasswordChangeView, self).form_valid(form)
Upvotes: 6
Reputation: 367
Just spent most of the day trying to achieve this. Eventually I found out it was pretty simple to implement it with a 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,
})
Upvotes: 10