Password1 and Password2 not show

hi there,
I just want to create a customized usercreationform, but fileds “password1” and “password2” did not show. Instead, the form shows “No password set.”.
I’m using django 2.2.16。

class MyUserCreationForm(forms.ModelForm):

password1 = forms.CharField(label='Password',
                            strip=False,
                            help_text="please input password",
                            widget=forms.PasswordInput(attrs={"class": "form-control",
                                                              "placeholder": "please input password"}))
password2 = forms.CharField(label='Password confirmation',
                            strip=False,
                            help_text="please input password again",
                            widget=forms.PasswordInput(attrs={"class": "form-control",
                                                              "placeholder": "please input password again"}))

class Meta:
    model = MyUser
    fields = ("username", "email")

def clean_password2(self):
    password1 = self.cleaned_data.get("password1")
    password2 = self.cleaned_data.get("password2")
    if password1 and password2 and password1 != password2:
        raise ValidationError("Passwords don't match")
    return password2

def save(self, commit=True):
    user = super(MyUserCreationForm, self).save(commit=False)
    user.set_password(self.cleaned_data["password1"])
    if commit:
        user.save()
    return user

class MyUserChangeForm(forms.ModelForm):

password = ReadOnlyPasswordHashField()

class Meta:
    model = MyUser
    fields = ("username", "email", "phone", "creator", "is_active", "password")

def clean_password(self):
    return self.initial["password"]

@admin.register(FvrUser)
class MyUserAdmin(admin.ModelAdmin):

form = MyUserChangeForm
add_form = MyUserCreationForm

fieldsets = (
    (None, {"fields": ("username", "email", "password")}),
    ("Personal info", {"fields": ("fullname", "phone")}),
    ("Permissions", {"fields": ("is_superuser", "is_active")})
)
add_fieldsets = (
    (None, {
        "classes": ("wide",),
        "fields": ("password1", "password2"),
    }),
)

Can you post the template and view you’re using to render the form?

Your “MyUserAdmin” class is inheriting from ModelAdmin instead of BaseUserAdmin.

The add_fieldsets setting is not a standard ModelAdmin attribute. See the full example in the docs.

1 Like