简体   繁体   中英

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. 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).

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:

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

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.

I usually pre-populate the form after the "POST" method, without specifying the request method (GET or POST). As I'm not prepopulating before the POST method, the prepopulate won't affect any new value that come with the POST method ; and as I not specifying any "GET" or "POST" method, if the POST method fails, it will prepopulate anyway.

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. You're overriding the user input by assigning to data .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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