Forms

django-password-policies ships two django.forms.Form classes that handles the validation of new passwords:

PasswordPoliciesForm

class password_policies.forms.PasswordPoliciesForm(user, *args, **kwargs)

A form that lets a user set his/her password without entering the old password.

Has the following fields and methods:

new_password1

PasswordPoliciesField (Required)

Parameters:
new_password2

CharField (Required)

Parameters:
  • error_messages – {u’required’: u’This field is required.’}
  • localize – False
  • widget – PasswordInput
error_messages = {u'password_mismatch': <django.utils.functional.__proxy__ object at 0x105074450>, u'password_used': <django.utils.functional.__proxy__ object at 0x10507c310>}

This forms error messages.

__init__(user, *args, **kwargs)

Initializes the form.

Parameters:user – A User instance.
clean_new_password1()

Validates that a given password was not used before.

clean_new_password2()

Validates that the two new passwords match.

save(commit=True)

Sets the user’s password to the new one and creates an entry in the user’s password history, if PASSWORD_USE_HISTORY is set to True.

PasswordPoliciesChangeForm

class password_policies.forms.PasswordPoliciesChangeForm(user, *args, **kwargs)

Bases: password_policies.forms.PasswordPoliciesForm

A form that lets a user change his/her password by entering their old password.

Has the following fields and methods:

old_password

CharField (Required)

Parameters:
  • error_messages – {u’required’: u’This field is required.’}
  • localize – False
  • widget – PasswordInput
new_password1

PasswordPoliciesField (Required)

Parameters:
new_password2

CharField (Required)

Parameters:
  • error_messages – {u’required’: u’This field is required.’}
  • localize – False
  • widget – PasswordInput
error_messages = {u'password_incorrect': <django.utils.functional.__proxy__ object at 0x10507c6d0>, u'password_mismatch': <django.utils.functional.__proxy__ object at 0x105074450>, u'password_used': <django.utils.functional.__proxy__ object at 0x10507c310>, u'password_similar': <django.utils.functional.__proxy__ object at 0x10507c710>, u'password_identical': <django.utils.functional.__proxy__ object at 0x10507c750>}

This forms error messages.

clean_old_password()

Validates the current password.

clean()

Validates that old and new password are not too similar.

PasswordPoliciesRegistrationForm

class password_policies.forms.PasswordPoliciesRegistrationForm(data=None, files=None, auto_id=u'id_%s', prefix=None, initial=None, error_class=<class 'django.forms.utils.ErrorList'>, label_suffix=None, empty_permitted=False)

A form to support user registration with password policies.

Has the following fields and methods:

username

RegexField (Required)

Parameters:
  • error_messages – {u’required’: u’This field is required.’, u’invalid’: u’This value may contain only letters, numbers and @/./+/-/_ characters.’}
  • help_text – Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.
  • localize – False
  • validators – [‘MaxLengthValidator‘, ‘RegexValidator‘]
  • widget – TextInput
password1

PasswordPoliciesField (Required)

Parameters:
password2

CharField (Required)

Parameters:
  • error_messages – {u’required’: u’This field is required.’}
  • help_text – Enter the same password as above, for verification.
  • localize – False
  • widget – PasswordInput
error_messages = {u'duplicate_username': <django.utils.functional.__proxy__ object at 0x10507cd10>, u'password_mismatch': <django.utils.functional.__proxy__ object at 0x10507cd50>}

This forms error messages.

clean_username()

Validates that the username is not already taken.

clean_password2()

Validates that the two passwords are identical.

PasswordResetForm

class password_policies.forms.PasswordResetForm(data=None, files=None, auto_id=u'id_%s', prefix=None, initial=None, error_class=<class 'django.forms.utils.ErrorList'>, label_suffix=None, empty_permitted=False)

A form to let a user reset his/her password.

Has the following fields and methods:

email

EmailField (Required)

Parameters:
  • error_messages – {u’required’: u’This field is required.’}
  • help_text – help
  • localize – False
  • validators – [‘EmailValidator‘, ‘MaxLengthValidator‘]
  • widget – EmailInput
error_messages = {u'unknown': <django.utils.functional.__proxy__ object at 0x10507ca10>, u'unusable': <django.utils.functional.__proxy__ object at 0x10507ca50>}

This forms error messages.

clean_email()

Validates that an active user exists with the given email address.

save(domain_override=None, subject_template_name=u'registration/password_reset_subject.txt', email_template_name=u'registration/password_reset_email.txt', email_html_template_name=u'registration/password_reset_email.html', use_https=False, from_email=None, request=None)

Generates a one-use only link for resetting password and sends to the user.

Parameters:
  • domain_override (str) – A string that changes the site name and domain if needed.
  • email_template_name (str) – A relative path to a template in the root of a template directory to generate the body of the mail.
  • email_html_template_name (str) – A relative path to a template in the root of a template directory to generate the HTML attachment of the mail.
  • from_email (str) – The email address to use as sender of the email.
  • request – A HttpRequest instance.
  • subject_template_name (str) – A relative path to a template in the root of a template directory to generate the subject of the mail.
  • use_https (bool) – Determines wether to use HTTPS while generating the one-use only link for resetting passwords.
get_context_data(request, domain_override, use_https)

Returns a dictionary with common context items.

Parameters:
  • request – A HttpRequest instance.
  • domain_override (str) – A string that changes the site name and domain if needed.
  • use_https (bool) – Determines wether to use HTTPS while generating the one-use only link for resetting passwords.