如何在Crispy Forms中实现密码类型字段?
我在Django中使用了crispy forms。你可以看到我把密码字段的类型设置成了密码。但是,当表单显示出来时,输入框还是明文显示,类型仍然是type="text"
。我是不是漏掉了什么技巧呢?
def __init__(self, *args, **kwargs):
super(UserRegistrationForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'post'
# turn off HTML5 validation
self.helper.attrs = {'novalidate': ''}
self.helper.form_show_labels = False
self.helper.layout = Layout(
Field('email_mobile', type="hidden"),
Fieldset(
'Enter a password',
PrependedText('password', '<i class="fa fa-key"></i>',
placeholder='Password',
autocomplete='off', type="password"),
),
)
1 个回答
1
PrependedText
不是一个简单的输入框,所以仅仅传递 type
是不够的。type
应该添加到小部件上,而不是输入框上。
所以试试这个:
PrependedText(
'password',
'<i class="fa fa-key"></i>',
placeholder='Password',
widget=PasswordInput,
autocomplete='off',
)