Disable autocomplete in admin field

1,508 views
Skip to first unread message

Erik Cederstrand

unread,
Nov 12, 2015, 5:47:21 AM11/12/15
to Django Users
Hello,

I have a model with a CharField named "username". When I edit a model instance in the admin, my browser likes to autocomplete the contents of the "username" field with my username for the Django site, regardless of what was entered previously.

Is there anything I can do to disable this behaviour, except for renaming the field to "username_PLEASE_DONT_AUTOCOMPLETE_SAFARI_IM_LOOKING_AT_YOU"?

Thanks,
Erik

Ezequiel Bertti

unread,
Nov 12, 2015, 7:34:12 AM11/12/15
to django...@googlegroups.com

In html:

<input type="email" name="email" autocomplete="off">

Um django you need to create a form for your model and set a field to username. In this field, set a widget like this:

username = forms.CharField(widget=forms.TextInput(attrs={'autocomplete': 'off'}))

Now you set a form on your model admin, like this example




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/DFF8D42F-D4F1-4686-942E-A906F919488B%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.



--
Ezequiel Bertti
E-Mail: ebe...@gmail.com
Cel: (21) 99188-4860

Erik Cederstrand

unread,
Nov 12, 2015, 8:42:11 AM11/12/15
to Django Users
Hi Ezequeil,

Thanks for the explanation! This worked, but defining a new widget from scratch meant that I lost the other helpful attributes (length, class etc.) that the admin adds for me. Instead, in the DRY spirit, I opted to just add this one extra attribute. Here's the relevant code:

class MyAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['username'].widget.attrs['autocomplete'] = 'off'

Erik
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACrQMYrb9aDDD1L%2BFvOtn90M6YDEuaVODoO-JWPqF0E5YiS9UA%40mail.gmail.com.

Ezequiel Bertti

unread,
Nov 12, 2015, 9:13:06 AM11/12/15
to django...@googlegroups.com
Great!

If you prefer to write some code and not implement new class. What you thing to override the get_form method on model admin?


def get_form(self, request, obj=None, **kwargs):
form = super(EmpresaAdmin, self).get_form(request, obj, **kwargs)
form.fields['username'].widget.attrs['autocomplete'] = 'off'
return form



For more options, visit https://groups.google.com/d/optout.

Erik Cederstrand

unread,
Nov 13, 2015, 4:19:38 AM11/13/15
to Django Users

> Den 12. nov. 2015 kl. 14.41 skrev Erik Cederstrand <erik+...@cederstrand.dk>:
>
> Hi Ezequeil,
>
> Thanks for the explanation! This worked, but defining a new widget from scratch meant that I lost the other helpful attributes (length, class etc.) that the admin adds for me. Instead, in the DRY spirit, I opted to just add this one extra attribute. Here's the relevant code:
>
> class MyAdminForm(forms.ModelForm):
> def __init__(self, *args, **kwargs):
> super().__init__(*args, **kwargs)
> self.fields['username'].widget.attrs['autocomplete'] = 'off'

Just to follow up, this didn't actually work for me, I just thought so. Apparently, all major browsers have go to great lengths to ignore any attempts to disable autocomplete, and any suggestions to actually get it working involves hidden fields, custom javascript and all sorts of hoops and tricks (see e.g. http://stackoverflow.com/questions/22661977/disabling-safari-autofill-on-usernames-and-passwords).

So it seems I'm stuck with either disabling autocomplete entirely in my browser, or having the username for my LDAP server overwritten with 'erik' every time I open that form. Infuriating.

Erik

Reply all
Reply to author
Forward
0 new messages