Home > Enterprise >  how to customize UserCreationForm in django
how to customize UserCreationForm in django

Time:12-12

Hi I am trying to clean up my usercreationform using widgets but its not working. The class is not being passed 'form-control' It does however work for model form. Not sure what I am doing wrong? I am new.

forms.py

class ProfileForm(UserCreationForm):
  class Meta:
    model=User
    fields = ('email', 'avatar', 'password1', 'password2')

    def __init__(self, *args, **kwargs):
     super(ProfileForm, self).__init__(*args, **kwargs)

     self.fields['email'].widget.attrs['class'] = 'form-control'
     self.fields['password1'].widget.attrs['class'] = 'form-control'
     self.fields['password2'].widget.attrs['class'] = 'form-control'

class CustomerForm(ModelForm):
 class Meta:
    model = Customer
    fields = ['name','email','phone_number' ]
    widgets={
    'name' :forms.TextInput(attrs={'class':'form-control'}),
    'email' :forms.TextInput(attrs={'class':'form-control'}),
    'phone_number' :forms.TextInput(attrs={'class':'form-control'}),
    }

CodePudding user response:

class ProfileForm(UserCreationForm):
 class Meta:
    fields = ('email', 'avatar', 'password1', 'password2')
    model = User

email = forms.CharField(widget=forms.EmailInput(attrs={'placeholder': 'Email',
                                                            'class': 'form-control',
                                                            }))

avatar = forms.ImageField(widget=forms.FileInput(attrs={'placeholder': 'Avatar',
                                                            'class': 'form-control',
                                                            }))
password1 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password',
                                                            'class': 'form-control',
                                                            }))
password2 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Confirm Password',
                                                            'class': 'form-control',
                                                            }))

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields['password2'].label = "Confirm Password"
  • Related