'Django Model Field for html5 Range Slider

I'm looking for a Django Model Field to render a HTML5 range slider like:

<input type="range" min="1" max="100" value="50">

This question comes close to Which Django Form Field can provide me a HTML output of <input type="range" />? but it asks for a form where I would search for an admin field for Django Admin.

Further this discussion shows as well how to use an IntegerField as range slider in forms: https://code.djangoproject.com/ticket/20674

Long story short, can I use forms in Django Admin, or is there already a specific range field?



Solution 1:[1]

You can implement a widget, just like Django implements widgets. For example the NumberInput widget [GitHub] looks like:

class NumberInput(Input):
    input_type = 'number'
    template_name = 'django/forms/widgets/number.html'

We can do this ourselves, for example with a:

# app_name/widgets.py

from django.forms.widgets import NumberInput

class RangeInput(NumberInput):
    input_type = 'range'

then you can use this widget with:

# app_name/forms.py

from app_name.widgets import RangeInput

class MyForm(forms.Form):
    myint = forms.IntegerField(widget=RangeInput)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henry Ecker