Django UserCreationForm with one password

You can override the __init__() method of your form and remove the field you want:

class UserRegistrationForm(UserCreationForm):
    email = forms.EmailField(max_length=200, help_text='Required')

    class Meta:
        model = User
        fields = ('username', 'email', 'password1')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        del self.fields['password2']

Important: Anyway, it is not a common practice to have only one field for password, because user can mistype it. And security level decreases a lot.


The reason your form has two password fields is because your form inherits two fileds from the parent class, UserCreationForm:

## Based on actual Django source code
class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(...)
    password2 = forms.CharField(...)

If you want to remove one of the fields, all you have to do is set it to None in your child class.

However, In Django 2.1, the default UserCreationForm uses password2 to validate the password against the sitewide validators defined in settings.AUTH_PASSWORD_VALIDATORS (source: link)

An easy way to retain password validation, while also removing password2, is to set password2 = None and define clean_password1:

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import password_validation

class UserRegistrationForm(UserCreationForm):
    email = forms.EmailField(max_length=200, help_text='Required')
    password2 = None

    class Meta:
        model = User
        fields = ('username', 'email', 'password1')

    def clean_password1(self):
        password1 = self.cleaned_data.get('password1')
        try:
            password_validation.validate_password(password1, self.instance)
        except forms.ValidationError as error:

            # Method inherited from BaseForm
            self.add_error('password1', error)
        return password1