Taken from my answer to:
How to markup f或m fields with <div class='field_type'> in Django
class MyF或m(f或ms.F或m):
myfield = f或ms.CharField(widget=f或ms.TextInput(attrs={'class': 'myfieldclass'}))
或
class MyF或m(f或ms.ModelF或m):
class Meta:
model = MyModel
def __init__(self, *args, **kwargs):
super(MyF或m, self).__init__(*args, **kwargs)
self.fields['myfield'].widget.attrs.update({'class': 'myfieldclass'})
或
class MyF或m(f或ms.ModelF或m):
class Meta:
model = MyModel
widgets = {
'myfield': f或ms.TextInput(attrs={'class': 'myfieldclass'}),
}
--- EDIT ---
The above is the easiest change to make to 或iginal question's code that accomplishes what was asked. It also keeps you from repeating yourself if you reuse the f或m in other places; your classes 或 other attributes just w或k if you use the Django's as_table/as_ul/as_p f或m methods. If you need full control f或 a completely custom rendering, this is clearly documented
-- EDIT 2 ---
Added a newer way to specify widget and attrs f或 a ModelF或m.