4

I've seen lots of answers on SO and none of them works in my case. My model form looks as follows:

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'}))

Still current_password, new_password and confirm_password are populating on the form. I've checked on Safari and Google Chrome and I still have the same issue. Can anyone tell me what am I doing wrong?

Shrikant Kakani
  • 1,371
  • 1
  • 15
  • 37

3 Answers3

2

In the Django documentation you will find what you need to solve your problem, I did some tests and it worked perfectly.

Solution found - 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

With modern browsers, I don't believe you will be able to achieve the behavior you're looking for. According to the Mozilla Developer Network,

... many modern browsers do not support autocomplete="off" for login fields.

  • if a site sets autocomplete="off" for a form, and the form includes username and password input fields, then the browser will still offer to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits this page.
  • if a site sets autocomplete="off" for username and password input fields, then the browser will still offer to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits this page.

This is the behavior in Firefox (since version 38), Google Chrome (since 34), and Internet Explorer (since version 11).

Joey Wilhelm
  • 5,051
  • 27
  • 34
0

It can be done by changing 'off' with 'new-password' in all of your fields / widget / attrs / autocomplete. It works for me.

EX :

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

Solution found here

PolRaguénès
  • 144
  • 2
  • 10