Skip to main content

PasswordChangeView

This class provides a view for authenticated users to change their password using a specialized form. It handles the form processing, updates the user's password, and ensures the user's session remains active by updating the session authentication hash after a successful change. The view redirects to a success URL upon completion and uses a customizable template for the password change interface.

Attributes

AttributeTypeDescription
form_class[Form](../../../forms/forms/form.md?sid=django_forms_forms_form) = PasswordChangeFormThe form class used for password changes, which must handle the logic for validating and saving the new password.
success_urlstring = reverse_lazy("password_change_done")The URL to redirect to after the password has been successfully changed.
template_namestring = "registration/password_change_form.html"The path to the template used to render the password change form.
titlestring = _("Password change")The localized title string displayed on the password change page.

Methods


get_form_kwargs()

@classmethod
def get_form_kwargs() - > dict

Retrieves the keyword arguments required to instantiate the password change form. This method injects the current authenticated user into the form arguments to ensure the password change is performed on the correct account.

Returns

TypeDescription
dictA dictionary containing the form instantiation arguments, including the current user object.

form_valid()

@classmethod
def form_valid(
form: [PasswordChangeForm](../forms/passwordchangeform.md?sid=django_contrib_auth_forms_passwordchangeform)
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Saves the new password and updates the session authentication hash to prevent the user from being logged out after the password change. This ensures the current session remains valid while invalidating all other active sessions for security.

Parameters

NameTypeDescription
form[PasswordChangeForm](../forms/passwordchangeform.md?sid=django_contrib_auth_forms_passwordchangeform)The validated form instance containing the new password data and the user object.

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)A redirect response to the success URL defined in the view.