Django: How do I add arbitrary html attributes to input fields on a form?

Solution 1:

Check this page

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

Solution 2:

Sorry for advertisment, but I've recently released an app (https://github.com/kmike/django-widget-tweaks) that makes such tasks even less painful so designers can do that without touching python code:

{% load widget_tweaks %}
...
<div class="field">
   {{ form.city|attr:"autocomplete:off"|add_class:"my_css_class" }}
</div>

or, alternatively,

{% load widget_tweaks %}
...
<div class="field">
   {% render_field form.city autocomplete="off" class+="my_css_class" %}
</div>