How do I set custom HTML attributes in django forms? How do I set custom HTML attributes in django forms? python python

How do I set custom HTML attributes in django forms?


You can change the widget on the CharField to achieve the effect you are looking for.

search_input = forms.CharField(_(u'Search word'), required=False)search_input.widget = forms.TextInput(attrs={'size': 10, 'title': 'Search',})


You can also try this:

search_input = forms.CharField(    _(u'Search word'),    required=False,    widget=forms.TextInput(attrs={'size': 10, 'title': 'Search',}))

This is documented in the section entitled Styling widget instances.


Old question, but for who-ever is looking for another alternative, there's also this:https://docs.djangoproject.com/en/1.6/topics/forms/modelforms/#overriding-the-default-fields

You can do something like this:

from django.forms import ModelForm, Textareafrom myapp.models import Authorclass AuthorForm(ModelForm):    class Meta:        model = Author        fields = ('name', 'title', 'birth_date')        widgets = {            'name': Textarea(attrs={'cols': 80, 'rows': 20}),        }