I have created this widget
class DateTimeWidget(forms.TextInput):
attr = {'class': 'datetimepicker'}
class Media:
js = ('js/jquery-ui-timepicker-addon.js',)
Then I use it on my form
class SessionForm(forms.ModelForm):
class Meta:
model = Session
def __init__(self, *args, **kwargs):
super(SessionForm, self).__init__(*args, **kwargs)
self.fields['start_time'].widget = DateTimeWidget()
self.fields['end_time'].widget = DateTimeWidget()
No css class
is applied to my fields (I'm expecting datetimepicker applied to both start_time
& end_time
).
I imagine I have put attr
at a wrong location. Where am I supposed to specify it ?
I see multiple problems:
To override the attrs, you need to override the init and update the attrs. Or you could give attrs when initializing the widget. see: https://github.com/django/django/blob/master/django/forms/widgets.py#L163
widgets can also be defined like:
.
class SessionForm(forms.ModelForm):
class Meta:
model = Session
widgets = {'start_time': DateTimeWidget(),
'end_time': DateTimeWidget()}