简体   繁体   English

Flask WTForms IntegerField

[英]Flask WTForms IntegerField

wtforms.IntegerField.data not passed to html template on a post method but on a get method it is. wtforms.IntegerField.data 不是在 post 方法上传递给 html 模板,而是在 get 方法上传递。

Replacing the IntegerField with StringField in code below does pass the data to the html template.在下面的代码中用 StringField 替换 IntegerField 确实会将数据传递给 html 模板。 What am I missing or doing wrong?我错过了什么或做错了什么?

class TestForm(FlaskForm):
    number = IntegerField('Number')


@app.route('/', methods=['POST', 'GET'])
def home():
    form = TestForm()
    if request.method == 'GET':
        form.number.data = 100
    if request.method == 'POST':
        form.number.data = 200
    return render_template('number.html', form=form)

I've been facing the same issue.我一直面临同样的问题。 A workaround I found is to set the value in the template.我发现的一种解决方法是在模板中设置值。 This is certainly not optimal, but it works for now.这当然不是最佳的,但它现在有效。

{% if form.number.data %}
{{ form.number(value=form.number.data) }}
{% else %}
{{ form.number() }}
{% endif %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM