Specify max and min in NumberInput widget
Question:
I have a form that is using NumberInput
widget to accept a rating for a Song from the user. The number should be between 0-5. My Song model has a MaxValueValidator
but I want the NumberInput
widget to show options only from 0-5.
Answers:
In your form you can add min and max value which atleast shows the user the value should be between the limits.
rating = forms.CharField(label='Rating', widget=forms.TextInput(attrs={'min':1,'max': '5','type': 'number'}))
As shown below, by setting "max"(for maximum number), "min"(for minimum number) to "forms.NumberInput()", you can do what you want to do:
class SongForm(forms.ModelForm):
class Meta:
widgets = {
'rate': forms.NumberInput(attrs={
'max': '5', # For maximum number
'min': '0', # For minimum number
}),
}
I have a form that is using NumberInput
widget to accept a rating for a Song from the user. The number should be between 0-5. My Song model has a MaxValueValidator
but I want the NumberInput
widget to show options only from 0-5.
In your form you can add min and max value which atleast shows the user the value should be between the limits.
rating = forms.CharField(label='Rating', widget=forms.TextInput(attrs={'min':1,'max': '5','type': 'number'}))
As shown below, by setting "max"(for maximum number), "min"(for minimum number) to "forms.NumberInput()", you can do what you want to do:
class SongForm(forms.ModelForm):
class Meta:
widgets = {
'rate': forms.NumberInput(attrs={
'max': '5', # For maximum number
'min': '0', # For minimum number
}),
}