Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Guy Scorpion
Guy Scorpion
11,053 Points

getting data from wtforms FormField

I have a form using FormField to display a related form I want to validate along with some other data.

class Calculator(Form):
    amount = IntegerField('Amount', validators=DataRequired() )
    weight = IntegerField('Weight', validators=DataRequired() )

class Program(Form):
    cycles = IntegerField('Cycles', validators=DataRequired() )
    volume = FormField(Calculator)

I want to grab some form data in my view but I'm not sure the syntax works in this situation. Example of how I thought it would work:

@app.route('/', methods=('GET', 'POST'))
def index():
    form = forms.Program()
    if form.validate_on_submit():
        values = models.Progression(
            cycles=form.cycles.data,
            amount=form.amount.data,
            weight=form.weight.data
        )
    return render_template('index.html', form=form, values=values)

This seems to get the data for cycles but not for amount and weight. Not really sure how to get the data from the encapsulated form.

3 Answers

Guy Scorpion
Guy Scorpion
11,053 Points

so the problem wasn't with how I was trying to call the data

amount = form.volume.amount.data

This works perfectly well. It was an issue with the form not validating when using FormField * face palm * I had to enable CSRF protection by adding

CsrfProtect(app)

to my app.py for the form to validate

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The form Program only has two fields: cycles and volume. The other fields amount and weight appear to be part of the other form Calculator and won't be available in forms.Program()

Edit: When using an Field Enclosure:

Parameters:

  • "form_class – A subclass of Form that will be encapsulated."
  • "separator – A string which will be suffixed to this field’s name to create the prefix to enclosed fields. The default is fine for most uses."

So try this:

amount=form.volume-amount.data,
weight=form.voulume-weight.data
Guy Scorpion
Guy Scorpion
11,053 Points
volume = FormField(Calculator)

FormField lets me include all the fields of Calculator when I call Program. So when I run my app all the right fields are there, I just don't know how to access the data.

At first I thought something like this would work:

amount = form.volume.amount.data

but that doesn't seem to return anything.

** EDIT: Answer is in a comment below.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I missed the bit on Field Enclosures. Updated my answer.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Another way to debug is to output the raw form data to see what it contains. This helps understand the structure.

print(form)