【发布时间】:2018-05-07 11:49:32
【问题描述】:
我有一个用于注册新用户的 Django / Bootstrap 简洁表单:
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput(
attrs={'autocomplete':'off',}))
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.layout = Layout(
Fieldset(
'',
'username',
'first_name',
'last_name',
'email',
'password'
),
ButtonHolder(
Submit('submit', 'Submit', css_class='button white')
)
)
问题是浏览器自动完成填写电子邮件和密码字段。 Django 脆皮表单围绕密码生成的 HTML 如下:
<div id="div_id_password" class="control-group">
<label for="id_password" class="control-label requiredField">
Password
<span class="asteriskField">*</span>
</label>
<div class="controls">
<input name="password" autocomplete="off" required="" id="id_password"
class="textinput textInput" type="password">
</div>
</div>
现在我知道关闭自动完成功能不起作用。所以我想做一个通常的技巧,通过 Django 脆表单在密码之前插入一个不可见字段 (display:none),结果是:
<div id="div_id_password" class="control-group">
<label for="id_password" class="control-label requiredField">
Password
<span class="asteriskField">*</span>
</label>
<div class="controls">
<input type="text" style="display:none;">
<input name="password" autocomplete="off" required="" id="id_password"
class="textinput textInput" type="password">
</div>
</div>
有没有一种很好的方法可以通过 Djano 清晰的表单插入此 HTML,除非有其他方法可以防止自动完成?
我正在使用 Firefox / Linux Mint。
【问题讨论】:
-
尝试在表单标签中设置
autocomplete="off"而不是输入:) -
@mohammedqudah 我使用以下方法设置表单属性:self.helper.attrs = {'autocomplete':'off',} 所以表单的自动完成功能已关闭,但它仍然没有任何效果.
-
您确定要添加此属性吗?检查代码。
-
是的,我确定,检查员显示:
-
哦,firefox 忽略
autocomplet="off"的密码,所以你现在的问题是 force set autocomplete in firefox input,我所知道的 :)
标签: django twitter-bootstrap django-crispy-forms