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!
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

Guy Scorpion
11,053 Pointsgetting 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
11,053 Pointsso 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
Treehouse Moderator 68,390 PointsThe 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
11,053 Pointsvolume = 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
Treehouse Moderator 68,390 PointsI missed the bit on Field Enclosures. Updated my answer.

Chris Freeman
Treehouse Moderator 68,390 PointsAnother way to debug is to output the raw form data to see what it contains. This helps understand the structure.
print(form)
Chris Freeman
Treehouse Moderator 68,390 PointsChris Freeman
Treehouse Moderator 68,390 PointsChanged comment to answer.