Django表单'autocomplete'='off'不起作用

时间:2015-10-13 22:46:25

标签: django google-chrome safari

我在SO上看到了很多答案,但在我的案例中没有一个可行。我的模型表格如下:

class ChangePasswordForm(forms.Form):
    current_password = forms.CharField(
        max_length=64,
        widget=forms.PasswordInput(
            attrs={'placeholder': 'Current Password', 'autocomplete': 'off'}))

    new_password = forms.CharField(
        min_length=6,
        max_length=64,
        widget=forms.PasswordInput(
            attrs={'placeholder': 'New Password', 'autocomplete': 'off'}))

    confirm_password = forms.CharField(
        min_length=6,
        max_length=64,
        widget=forms.PasswordInput(
            attrs={'placeholder': 'Confirm New Password', 'autocomplete': 'off'}))

仍在表格中填充了current_password,new_password和confirm_password。我已经检查过Safari和Google Chrome,但我仍然遇到同样的问题。谁能告诉我我做错了什么?

3 个答案:

答案 0 :(得分:2)

在Django文档中,您将找到解决问题所需要的东西,我进行了一些测试,并且效果很好。

找到解决方案-Django documentation

    from django import forms

    class ChangePasswordForm(forms.Form):
        password = forms.CharField(label='Password', widget=forms.PasswordInput)
        confirmPass = forms.CharField(label='Confirm Password', widget=forms.PasswordInput)

        password.widget.attrs.update({'autocomplete':'off', 'maxlength':'32'})
        confirmPass.widget.attrs.update({'autocomplete':'off', 'maxlength':'32'})

答案 1 :(得分:1)

对于现代浏览器,我不相信您将能够实现您正在寻找的行为。根据{{​​3}},

  

...许多现代浏览器不支持登录时使用autocomplete =“off”   字段。

     
      
  • 如果网站为表单设置autocomplete =“off”,并且表单包含用户名和密码输入字段,那么浏览器仍然会提供记住此登录信息,如果用户同意,浏览器将自动填充这些字段下次用户访问此页面时。
  •   
  • 如果网站为用户名和密码输入字段设置了autocomplete =“off”,则浏览器仍然会提供记住此登录信息,如果用户同意,浏览器将在用户下次访问此页面时自动填充这些字段
  •   
     

这是Firefox(自版本38以来),Google Chrome(自34版本)和Internet Explorer(自版本11以来)以来的行为。

答案 2 :(得分:0)

可以通过在所有字段/ widget / attrs / autocomplete中使用'new-password'更改'off'来完成。它对我有用。

EX:

current_password = forms.CharField(
    max_length=64,
    widget=forms.PasswordInput(
        attrs={'placeholder': 'Current Password', 'autocomplete': 'new-password'}))

找到解决方案here

相关问题