pythonpython-3.xdjangodjango-formsdjango-widget

Specify max and min in NumberInput widget


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.


Solution

  • 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'}))