SetPasswordMixin
Form mixin that validates and sets a password for a user.
Attributes
| Attribute | Type | Description |
|---|---|---|
| error_messages | dict = {"password_mismatch": "The two password fields didn’t match."} | A dictionary mapping error codes to localized error messages used during password validation, specifically for reporting password mismatches. |
Methods
create_password_fields()
@classmethod
def create_password_fields(
label1: string = Password,
label2: string = Password confirmation
) - > tuple
Generates a pair of Django form fields for password entry and confirmation with standard security attributes.
Parameters
| Name | Type | Description |
|---|---|---|
| label1 | string = Password | The display label for the primary password input field. |
| label2 | string = Password confirmation | The display label for the secondary password verification field. |
Returns
| Type | Description |
|---|---|
tuple | A tuple containing two CharField instances configured with PasswordInput widgets. |
validate_passwords()
@classmethod
def validate_passwords(
password1_field_name: string = password1,
password2_field_name: string = password2
) - > null
Compares two password fields in the form's cleaned data to ensure they are identical.
Parameters
| Name | Type | Description |
|---|---|---|
| password1_field_name | string = password1 | The key in cleaned_data representing the primary password field. |
| password2_field_name | string = password2 | The key in cleaned_data representing the confirmation password field. |
Returns
| Type | Description |
|---|---|
null | null |
validate_password_for_user()
@classmethod
def validate_password_for_user(
user: [User](../models/user.md?sid=django_contrib_auth_models_user),
password_field_name: string = password2
) - > null
Runs Django's password validation suite against a specific user instance to enforce complexity and security rules.
Parameters
| Name | Type | Description |
|---|---|---|
| user | [User](../models/user.md?sid=django_contrib_auth_models_user) | The user model instance against which the password complexity is validated. |
| password_field_name | string = password2 | The key in cleaned_data containing the password string to be validated. |
Returns
| Type | Description |
|---|---|
null | null |
set_password_and_save()
@classmethod
def set_password_and_save(
user: [User](../models/user.md?sid=django_contrib_auth_models_user),
password_field_name: string = password1,
commit: boolean = True
) - > [User](../models/user.md?sid=django_contrib_auth_models_user)
Updates the user's password with the hashed value from the form and optionally persists the user to the database.
Parameters
| Name | Type | Description |
|---|---|---|
| user | [User](../models/user.md?sid=django_contrib_auth_models_user) | The user model instance whose password will be updated. |
| password_field_name | string = password1 | The key in cleaned_data containing the plain-text password to hash. |
| commit | boolean = True | Determines whether to call the save() method on the user instance after setting the password. |
Returns
| Type | Description |
|---|---|
[User](../models/user.md?sid=django_contrib_auth_models_user) | The updated user model instance. |