Skip to content Skip to sidebar Skip to footer

How Do I Correctly Submit Results From A Wtformusing Jinja With A Python /flask Setup

What I'm trying to do Instead of using form.predictions() I've tried to separate it out so I can style it better, although it doesn't work when I submit the predictions apart from

Solution 1:

I believe the problem is in this block of code:

if request.method == 'POST':
    for prediction in form.predictions:
        store=db.session.query(Fixture_prediction) \
            .filter(Fixture_prediction.user_id==user_id) \
            .filter(Fixture_prediction.fixture_id==prediction.fixture_id.data)\
            .update({'home_score':prediction.home_score.data\
            ,'away_score':prediction.away_score.data})
        db.session.commit()
        flash('Prediction added')
        return redirect(url_for('predictions'))

The problem is after the first iteration of your for loop, you commit your changes, flash a message and redirect. These three statements need to be outside the for loop like below:

if request.method == 'POST':
    for prediction in form.predictions:
        store=db.session.query(Fixture_prediction) \
            .filter(Fixture_prediction.user_id==user_id) \
            .filter(Fixture_prediction.fixture_id==prediction.fixture_id.data)\
            .update({'home_score':prediction.home_score.data\
            ,'away_score':prediction.away_score.data})
    db.session.commit()
    flash('Prediction added')
    return redirect(url_for('predictions'))

Post a Comment for "How Do I Correctly Submit Results From A Wtformusing Jinja With A Python /flask Setup"