简体   繁体   English

预填充Flask-WTForm IntegerField

[英]Prepopulate Flask-WTForm IntegerField

Simple form: 简单的形式:

class AdjustPWMForm(Form):
    dutyCycle = IntegerField('dutycycle')
    #dutyCycle = IntegerField('dutycycle', default=44)

View function: 查看功能:

 def adjust():
    user = g.user
    form = AdjustPWMForm()
    form.dutyCycle.data = 55
    if form.validate_on_submit():
        dutyCycle = form.dutyCycle.data
        print('result %s', dutyCycle)
        return redirect(url_for('statusPage'))

    return render_template('adjust.html', title='Adjust PWM', user=user, form=form)

When the form is submitted, the result is always 55, regardless of what has been entered. 提交表单后,无论输入什么,结果始终为55。 If I comment out the 55 line, and uncomment the line with the default=44, the form works properly, but I really need to be able to set the prepopulate value (it will be retrieved from the database). 如果我注释掉55行,并用default = 44取消注释该行,则该表单可以正常工作,但是我确实需要能够设置预填充值(将从数据库中检索该值)。

It seems like I'm missing something really obvious, but I've hunted around and can't figure it out. 似乎我确实遗漏了一些明显的东西,但是我四处寻找,无法解决。

I found the section in change_username useful in wtforms which leads me to: 我发现在change_username有用的部分wtforms这使我:

def adjust():
    user = g.user
    form = AdjustPWMForm(dutyCycle=55)
    if form.validate_on_submit():
        dutyCycle = form.dutyCycle.data
        return redirect(url_for('statusPage'))

    return render_template('adjust.html', title='Adjust PWM', user=user, form=form)

Is that the best way to accomplish this? 那是实现这一目标的最佳方法吗?

You could set the default value on a GET request 您可以在GET请求上设置默认值

def adjust():
    user = g.user
    form = AdjustPWMForm()
    if request.method == 'GET':
        form.dutyCycle.data = 55
    if form.validate_on_submit():
        dutyCycle = form.dutyCycle.data
        print('result %s', dutyCycle)
        return redirect(url_for('statusPage'))
    return render_template('adjust.html', title='Adjust PWM', user=user, form=form)

or set a default value in the class 或在类中设置默认值

class AdjustPWMForm(Form):
    dutyCycle = IntegerField('dutycycle', default=55)

I would set the default in the class definition if the default works across all instances of this form. 如果默认值适用于此表单的所有实例,则可以在类定义中设置默认值。 If I need to set a default value for a field based on some other lookup, I would set it using the form = AdjustPWMForm(dutyCycle=55) way. 如果需要基于其他查找为字段设置默认值,则可以使用form = AdjustPWMForm(dutyCycle=55)形式进行设置。

I usually pre-populate the form after the "POST" method, without specifying the request method (GET or POST). 我通常在“ POST”方法之后预先填充表单,而不指定请求方法(GET或POST)。 As I'm not prepopulating before the POST method, the prepopulate won't affect any new value that come with the POST method ; 由于我没有在POST方法之前进行预填充,因此预填充不会影响POST方法附带的任何新值; and as I not specifying any "GET" or "POST" method, if the POST method fails, it will prepopulate anyway. 并且由于我未指定任何“ GET”或“ POST”方法,因此,如果POST方法失败,它将仍然预先填充。

def foo():
    form = MyForm(request.form)

    if request.method == 'POST' and form.validate:
        my_code

    form.dutyCycle.data = 55

    render_template('my_template', form=form)

instead of 代替

form.dutyCycle.data = 55

use 采用

form.dutyCycle.process_data(55)

The data attribute holds the user placed data after validation. data属性保存验证后用户放置的数据。 You're overriding the user input by assigning to data . 您通过分配给data来覆盖用户输入。

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

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