Prepopulate Flask-WTForm IntegerField
Asked Answered
T

3

5

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?

Tafoya answered 13/10, 2014 at 15:12 Comment(0)
A
8

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.

Atonsah answered 13/10, 2014 at 19:52 Comment(1)
Thanks, your suggestion of setting the value only on GET, it works nicely. BTW, small typo...should be request.method.Tafoya
T
1

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)
Taxis answered 11/6, 2016 at 22:17 Comment(2)
I just noticed with your comment this post was about 2 years old. Sorry for the inconvenience.Taxis
Sorry, it popped up in the review queue. Looks good now! Thanks :)Paff
X
0

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.

Xenogamy answered 14/10, 2014 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.