2

I have a problem getting data from a form using it in a route

forms.py

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

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

app.py

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

The data for cycles comes through just fine but I am unsure of the syntax for how to access the encapsulated form within my route. The docs say FormField will return the data dict of the enclosed form but I can't seem to figure out how to grab that and put it in a variable.

2
  • So what's your problem? Be specific. Commented Apr 13, 2016 at 4:40
  • Sorry, I accidentally posted before I finished the question. Commented Apr 13, 2016 at 4:42

2 Answers 2

2

I was able to grab the data I needed using

    amount=form.volume.amount.data,
    weight=form.volume.weight.data

The real issue came from the fact that the form was not validating when I was using FormField. A rookie error I should have checked earlier. I had to enable CSRF protection by importing it from flask_wtf and using CsrfProtect(app)

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that the form data does not come through as the attributes of the Calculator class. The data is sent as a dictionary from the volume attribute.

Test it out with: print form.volume.data

(I suggest commenting out your values object and just use print statements)

The output should be: {'amount': foo, 'weight': bar}

Thanks for teaching me something! I never knew of FormField.

1 Comment

Thanks for that testing tip. It is what lead me to the real cause of my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.