在wtforms.fields.html5.DecimalRangeField旁边显示滑块值



我试图在wtforms.fields.html5.DecimalRangeField旁边显示滑块值。我当前的代码(下面的相关摘录)只呈现滑块,没有值。到目前为止,我看到的所有例子都是纯HTML5代码,我缺乏如何使用我的jinja2模板作为起点来实现这一点的指导。

有什么建议吗?

摘录自main.py:

class MyForm(Form):
    MyField = DecimalRangeField('Age', [validators.NumberRange(min=1, max=100)])

摘录自form.html

<div>{{ wtf.form_field(form.MyField) }}</div>

试试这个代码(要点):

from flask import Flask, render_template_string
from wtforms import Form
from wtforms.fields.html5 import DecimalRangeField

app = Flask(__name__)
app.config['DEBUG'] = True
TPL = '''
<!DOCTYPE html>
<html>
<head>
<script>
function outputUpdate(age) {
    document.querySelector('#selected-age').value = age;
}
</script>
</head>
<body>
<form>
    <p>
       {{ form.age.label }}:
       {{ form.age(min=0, max=100, oninput="outputUpdate(value)") }}
       <output for="age" id="selected-age">{{ form.age.data }}</output>
    </p>
</form>
</body>
</html>
'''
class TestForm(Form):
    age = DecimalRangeField('Age', default=0)

@app.route("/")
def home():
    form = TestForm(csrf_enabled=False)
    return render_template_string(TPL, form=form)

if __name__ == "__main__":
    app.run()

最新更新