【发布时间】:2021-08-31 04:18:57
【问题描述】:
我正在尝试使用引导程序对我的表单进行风格化。如您所知,bootstrap 使用很多类来完成它的工作。通过谷歌搜索,我发现在我的表单中注入了一些新类,我可以将小部件与 django 一起使用。我的表格如下:
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
email = forms.EmailField()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['password1'].label = 'Password'
self.fields['password2'].label = 'Password Confirmation'
self.fields['first_name'].label = 'First Name'
self.fields['last_name'].label = 'Last Name'
self.fields['password1'].help_text = None
self.fields['password2'].help_text = None
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2' )
help_texts = {
'username': None,
}
widgets = {
'username': forms.TextInput(
attrs={
'class': 'form-control'
}
),
'first_name': forms.TextInput(
attrs={
'class': 'form-control'
}
),
'last_name': forms.TextInput(
attrs={
'class': 'form-control'
}
),
'email': forms.EmailInput(
attrs={
'class': 'form-control'
}
),
'password1': forms.PasswordInput(
attrs={
'class': 'form-control'
}
),
'password2': forms.PasswordInput(
attrs={
'class': 'form-control'
}
)
}
但由于某种原因,这只是将类应用于 HTML 中的用户名字段。它不会将“表单控制”类应用于其他任何东西。这是我忽略的一些简单格式问题还是我做错了什么?非常感谢任何帮助!
【问题讨论】:
标签: python django bootstrap-4 django-widget