Search code examples
pythonflaskwtforms

Flask WTForms Integerfield type is text instead of number


This is what I have tried:

nrkomp = IntegerField('Number',validators=[NumberRange(min=1, max=5, message='Invalid length')])

In developer tools, this form input has type text and not number, I have read the docs, but could not find a solution to this problem.


Solution

  • You can use wtforms html5 fields to get html5 input types, and html5 widgets as their associated widgets.

    from wtforms import Form
    from wtforms.fields import html5 as h5fields
    from wtforms.widgets import html5 as h5widgets
    
    
    class F(Form):
    
        n1 = h5fields.IntegerField("Number1")
        n2 = h5fields.IntegerField(
            "Number2", widget=h5widgets.NumberInput(min=0, max=100, step=10)
        )
    
    
    for f in F():
        print(f)
    
    <input id="n1" name="n1" step="1" type="number" value="">
    <input id="n2" max="100" min="0" name="n2" step="10" type="number" value="">