I am using forms in my Django project, and I want to specify some of the attributes of the widget that I use in my form but am having trouble figuring out how to pass in the attrs
dictionary to the widget.
The view.py:
form_schedule_start = WINDOW_Schedule_Form(section_label=" Start",required=True,initial=scheduleStart,attributes={'class':"form-control",'placeholder':".col-md-4"})
The form:
class WINDOW_Schedule_Form(forms.Form):
def __init__(self,*args,**kwargs):
section_label = kwargs.pop('section_label')
initial_value = kwargs.pop('initial')
required_value = kwargs.pop('required')
attributes = kwargs.pop('attributes')
super(WINDOW_Schedule_Form,self).__init__(*args,**kwargs)
self.fields['WINDOW_Schedule'].label=mark_safe(section_label)
self.fields['WINDOW_Schedule'].initial=initial_value
self.fields['WINDOW_Schedule'].required=required_value
self.fields['WINDOW_Schedule'].attrs=attributes
WINDOW_Schedule = forms.CharField(widget=forms.TextInput())
Normally you would just do
WINDOW_Schedule = forms.CharField(widget=forms.TextInput(attrs={'class':"form-control text-center",'placeholder':".col-md-8"}))
but I want to be able to specify the 'class'
and 'placeholder'
attributes in my views.py.
I keep getting an error that attributes
is not defined though. Can anyone tell me what I'm doing wrong?