您應該覆蓋類Meta上的字段。這適用於我:
class CustomCreateUserForm(UserCreationForm):
username = forms.RegexField(
label=_("Login"), max_length=30, regex=r"^[\[email protected]+-]+$",
help_text=_("Required. 30 characters or fewer. Letters, digits and "
"@/./+/-/_ only."),
error_messages={
'invalid': _("This value may contain only letters, numbers and "
"@/./+/-/_ characters.")},
widget=TextInput(attrs={'class': 'form-control',
'required': 'true',
'placeholder': 'Login'
})
)
password1 = forms.CharField(
label=_("Password"),
widget=forms.PasswordInput(attrs={'class': 'form-control',
'required': 'true',
})
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput(attrs={'class': 'form-control',
'type': 'password',
'required': 'true',
}),
help_text=_("Enter the same password as above, for verification.")
)
first_name = forms.CharField(
label=_("Name"),
widget=forms.TextInput(attrs={'class': 'form-control',
'type': 'text',
'required': 'true',
}),
help_text=_("Enter user first and last name.")
)
email = forms.CharField(
label=_("Email"),
widget=forms.TextInput(attrs={'class': 'form-control',
'type': 'email',
'placeholder': 'Email address',
'required': 'true'
})
)
class Meta:
model = User
您確定已經包含[表單資產](https://docs.djangoproject.com/zh/dev/topics/forms/media/)嗎?你能發佈你正在使用的完整模板嗎? – arocks
爲什麼我需要表單資源,我在模板中包含了css和js,但這些小部件沒有獲得該類。是不是用於定義CSS和JS的資產? – Apostolos
我誤解了CSS類沒有得到渲染。您是否嘗試使用列出表單字段的聲明性表單? – arocks