[Answered ]-Unable to change password in Django with custom user model

1👍

After the comments telling me the problem wasn’t reproducible I found the issue : a save function I hadn’t included in the problem.

    def save(self, *args, **kwargs):
    if not self.pk:
        super().save(*args, **kwargs)
        try:
            self.base_group.user_set.add(self)
        except:
            pass

This was supposed to make sure a user always belonged to the right group, however it interfered with the password change. Thank you to all of those who commented and answered.

0👍

It seems that you have correctly defined a custom user model in Django and have implemented a custom password change view. However, there are a few modifications you can make to ensure the password change process works as expected.

  1. Remove the set_password method in your CustomUser model:
    The set_password method you implemented in your CustomUser model is unnecessary because Django’s built-in AbstractBaseUser class already provides a set_password method. By overriding it in your custom user model, you may be introducing unintended behavior. Remove the set_password method from your CustomUser model.
  2. Use Django’s built-in PasswordChangeView directly:
    Instead of creating a custom password change view, you can use Django’s built-in PasswordChangeView directly in your URL configuration. Remove the CustomPasswordChangeView and update your urls.py file as follows:
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('password_change/', auth_views.PasswordChangeView.as_view(template_name="registration/password_change_form.html", success_url=reverse_lazy("password_change_done")), name='password_change'),
    path('password_change_done/', auth_views.PasswordChangeDoneView.as_view(template_name="registration/password_change_done.html"), name='password_change_done'),
    # Other authentication URLs
]
  1. Ensure the URL patterns are correct:
    Make sure you have included the authentication URLs correctly in your project’s urls.py file. Confirm that the urlpatterns list includes the path for the password_change_done view, as shown in the example above.
  2. Check your registration/password_change_form.html template:
    Ensure that the form in your password_change_form.html template correctly submits the old password, new password, and confirmation. Make sure the form fields are named appropriately (old_password, new_password1, new_password2) to match Django’s default behavior.

After making these modifications, try the password change process again and see if the issue is resolved.

Leave a comment