36π
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.
37π
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>
- [Django]-Django: Get model from string?
- [Django]-Organizing Django unit tests
- [Django]-Django Sitemaps and "normal" views
13π
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'}),
}
- [Django]-Override django's model delete method for bulk deletion
- [Django]-ValueError: Unable to configure handler 'file': [Errno 2] No such file or directory:
- [Django]-Can't connect to local MySQL server through socket '/tmp/mysql.sock
1π
For me adding extra attribute in templates also worked:
<form method="POST", autocomplete="off">
{% csrf_token %}
{{ form.as_p }}`
- [Django]-How to deploy an HTTPS-only site, with Django/nginx?
- [Django]-Django Test Client Method Override Header
- [Django]-Can I have a Django model that has a foreign key reference to itself?
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"
- [Django]-Django.db.utils.ProgrammingError: relation already exists
- [Django]-Django β No such table: main.auth_user__old
- [Django]-How to change empty_label for modelForm choice field?