我使用基于UserCreationForm的字段username,password1和pasword2创建了一个自定义用户表单。
我想在Meta小部件中使用bootstrap表单控件来设置它,但它不起作用。
这可能吗?
修改
虽然password1和password2不在Meta字段中,但它们会被渲染。
代码:
class UserForm(UserCreationForm):
class Meta:
model = User
fields = ['username']
widgets = {
'username': TextInput(attrs = {
'class': 'form-control',
}),
'password1': PasswordInput(attrs = {
'class': 'form-control',
}),
'password2': PasswordInput(attrs = {
'class': 'form-control',
}),
}
答案 0 :(得分:0)
解决这个问题:
class UserForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
self.fields['username'].widget = TextInput(attrs = {'class': 'form-control',})
self.fields['password1'].widget = PasswordInput(attrs = {'class': 'form-control',})
self.fields['password2'].widget = PasswordInput(attrs = {'class': 'form-control',})
class Meta:
model = User
fields = UserCreationForm.Meta.fields