28

Does anyone know how you can turn off autocompletion on a textfield in Django?

For example, a form that I generate from my model has an input field for a credit card number. It is bad practice to leave autocompletion on. When making the form by hand, I'd add a autocomplete="off" statement, but how do you do it in Django and still retain the form validation?

tau-neutrino
  • 3,200
  • 6
  • 23
  • 20

5 Answers5

35

In your form, specify the widget you want to use for the field, and add an attrs dictionary on that widget. For example (straight from the django documentation):

class CommentForm(forms.Form):
    name = forms.CharField(
                widget=forms.TextInput(attrs={'class':'special'}))
    url = forms.URLField()
    comment = forms.CharField(
               widget=forms.TextInput(attrs={'size':'40'}))

Just add 'autocomplete': 'off' to the attrs dict.

Zags
  • 31,395
  • 12
  • 87
  • 123
BJ Homer
  • 48,278
  • 10
  • 114
  • 129
  • Thanks! I found this 1 minute before your answer...isn't that how it always goes?! – tau-neutrino Apr 05 '10 at 21:05
  • 6
    This doesn't work for me in Chrome. It still autocompletes username and password fields, even when autocomplete=off. – Cerin May 28 '14 at 19:32
  • 2
    take a look at this question: http://stackoverflow.com/questions/15738259/disabling-chrome-autofill – codescribblr Apr 21 '17 at 14:44
  • 3
    Since `attrs` is a dictionary it should be `'autocomplete': 'off'`. – filiphl Aug 12 '18 at 11:34
  • Just for the sake of finding easily the answer : the link shared by @codescribblr proposes to replace "off" by "new-password". Worked for me (while all other solutions didn't) – PolRaguénès Mar 02 '19 at 21:21
33

Add the autocomplete="off" to the form tag, so you don't have to change the django.form instance.

<form action="." method="post" autocomplete="off"> {{ form }} </form>

jjlorenzo
  • 363
  • 3
  • 2
12

If you are defining your own forms, you can add attributes to your fields in the form.

class CommentForm(forms.Form):
    name = forms.CharField(widget=forms.TextInput(attrs={
        'autocomplete':'off'
    }))

If you are using modelforms, you won't have the luxury of defining field attributes in the form. However, you can use __init__ to add required attributes.

class CommentForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(CommentForm, self).__init__(*args, **kwargs)
        self.fields['name'].widget.attrs.update({
            'autocomplete': 'off'
        })

You can also add attributes from Meta

class CommentForm(forms.ModelForm):
    class Meta:
        widgets = {
            'name': TextInput(attrs={'autocomplete': 'off'}),
        }
Chillar Anand
  • 25,302
  • 8
  • 106
  • 124
0

For me adding extra attribute in templates also worked:

<form method="POST", autocomplete="off">
    {% csrf_token %}
    {{ form.as_p }}`
Chillar Anand
  • 25,302
  • 8
  • 106
  • 124
Bartosz
  • 29
  • 4
0

if you are using django inbuilt forms for example login forms, or usercreationform as opposed to your own custom model forms, then you will have to remove autocomplete in javascript

const username = document.getElementById("id_username");
username.autocomplete = "off"
Glauco
  • 1,097
  • 2
  • 8
  • 18