我使用AuthenticationForm
类登录用户,使用UserCreationForm
类允许用户创建帐户。当我只是呈现默认表单时(在HTML文件中使用{{ form.as_p }}
),一切都正常。然而,我想让表单看起来更好,所以我已经覆盖了表单如下:
class LoginForm(AuthenticationForm):
username = forms.CharField(required=True, widget=TextInput(attrs={
'class': "block border border-grey-light w-full p-3 rounded mb-4",
"name": "username", "placeholder": "Username"}))
password = forms.CharField(widget=PasswordInput(attrs={
'class': "block border border-grey-light w-full p-3 rounded mb-4",
"name": "password", "placeholder": "Password"}))
class RegistrationForm(UserCreationForm):
email = forms.EmailField(required=True, widget=EmailInput(attrs={
'class': "block border border-grey-light w-full p-3 rounded mb-4",
"name": "email", "placeholder": "Email"}))
password1 = forms.CharField(widget=PasswordInput(attrs={
'class': "block border border-grey-light w-full p-3 rounded mb-4",
"name": "password1", "placeholder": "Password"}))
password2 = forms.CharField(widget=PasswordInput(attrs={
'class': "block border border-grey-light w-full p-3 rounded mb-4",
"name": "password2", "placeholder": "Confirm password"}))
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']
widgets = {
'username': TextInput(attrs={'class': "block border border-grey-light w-full p-3 rounded mb-4",
"type": "text", "name": "username", "placeholder": "Username"}),
'first_name': TextInput(attrs={'class': "block border border-grey-light w-full p-3 rounded mb-4",
"type": "text", "name": "first_name", "placeholder": "First Name"}),
'last_name': TextInput(attrs={'class': "block border border-grey-light w-full p-3 rounded mb-4",
"type": "text", "name": "last_name", "placeholder": "Last Name"}),
}
然而,这样做似乎失去了这些表单提供的一些开箱即用的功能,例如UserCreationForm
检查password1
和password2
是否相同,并向用户返回一条消息。
我做错什么了吗?最后,我想做的就是保留这些表单附带的所有功能,但只添加tailwind类"block border border-grey-light w-full p-3 rounded mb-4"
。
1条答案
按热度按时间ws51t4hk1#
您可以像这样覆盖内置的**
UserCreationForm
**。在这里,我应用了Bootstrap表单控件类来设置输入样式form.py
您可以在**
views.py
**中这样使用注意-对所有执行相同操作