Issue
I am trying to make a form in Django using model forms but i am facing a problem. Following is my code in forms.py which has an IntegerField()
, I want to set a custom id and class for it but I'm not able to do so with the help of attrs
in widget of IntegerField()
whereas for CharField()
it works.
forms.CharField(widget=forms.CharField(attrs={'id': 'customID'}))
works but
hours = forms.IntegerField(widget=forms.IntegerField(attrs={'id': 'blah', }))
doesn't.
forms.py
class TimerForm(forms.ModelForm):
hours = forms.IntegerField(required=True, min_value=0, widget=forms.IntegerField(attrs={'id': 'blah', }))
class Meta:
model = Timer
fields = ['hours']
Error in cmd:
super(IntegerField, self).init(*args, **kwargs)
TypeError: init() got an unexpected keyword argument 'attrs'
Solution
You can't use fields there. You should use the relevant widgets: TextInput and NumberInput.
Answered By - Daniel Roseman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.