如何在django中自定义UserCreationForm

inkz8wg9  于 2022-12-14  发布在  Go
关注(0)|答案(1)|浏览(133)

你好,我试图清理我的usercreationform使用小部件,但它不工作。类没有被传递'形式控制',但它的工作模型形式。不知道我做错了什么?我是新的。
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'}),
    }
vtwuwzda

vtwuwzda1#

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"

相关问题