Saturday, 17 September 2016

python - Disable autocomplete on textfield in Django?



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?


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...