Flask WTForms Integerfield type is text instead of number

╄→尐↘猪︶ㄣ 提交于 2020-05-15 06:14:48

问题


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.


回答1:


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="">


来源:https://stackoverflow.com/questions/53449992/flask-wtforms-integerfield-type-is-text-instead-of-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!