How do you store the request.form to db through wtforms or error in sqlalchemy update?

假装没事ソ 提交于 2019-12-13 04:23:20

问题


This is following on from this question: SQLalchemy/wtforms update issue - 400 bad request I have a flask framework

Issue

When I submit the form the flash message comes up saying prediction added although when I query the db nothing has changed?? Can anyone spot where I'm going wrong?

What am I trying to achieve

Users are able to view their predictions making changes to current predictions. If there are no new predictions then they can submit new predictions.

views

# Predictor - User makes predictions and saves/
@app.route('/predictor/',methods=['GET','POST'])
@login_required
def predictions():
    user_id = g.user.id
    # retrieve predictions
    prediction= db.session.query(Fixture_prediction,\
                Fixture_prediction.fixture_id,Fixture.stage,\
                Fixture.home_team,Fixture_prediction.home_score,\
                Fixture_prediction.away_score,Fixture.away_team)\
                .outerjoin(Fixture,Fixture.id==Fixture_prediction.fixture_id)\
                .outerjoin(User,Fixture_prediction.user_id == User.id)\
                .filter(Fixture_prediction.fixture_id==Fixture.id)\
                .filter(Fixture_prediction.user_id==user_id).all()
    data = {'predictions': prediction}
    form = PredictionListForm(data=MultiDict(data))
    if request.method == 'POST':
        if form.validate() == False:
            flash('A score is missing, please fill in all predictions')
            render_template('predictor.html', form=form)
        else:
            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'))
    # display current predictions
    elif request.method == 'GET':
        return render_template('predictor.html', form=form)

thoughts I have a feeling the below is just submitting what was already there in the first place and not the form request...

.update({'home_score':prediction.home_score.data\
                     ,'away_score':prediction.away_score.data})

template

{% extends "base.html" %}

{% block content %}

  <h1>Predictions</h1>
  <p></p>
  <p>Please make your predictions here</p>
  <form action='' method='post'>
    {{form.predictions()}}
    <p><input type="submit" value="Submit Predictions"></p>
   </form>

{% endblock %}

template - alternative

{% extends "base.html" %}

{% block content %}

  <h1>Predictions</h1>
  <p></p>
  <p>Please make your predictions here</p>
   <form action='' method='post'>
   <table>
       {%for form in form.predictions%}
        <tr>
            <td>{{form.fixture_id.data}}</td>
            <td>{{form.stage.data}}</td>
            <td>{{form.home_team.data}}</td>
            <td>{{form.home_score(size=1)}}</td>
            <td>{{form.away_score(size=1)}}</td>
            <td>{{form.away_team.data}}</td>
        </tr>
       {%endfor%}
   </table>
   <p><input type="submit" value="Submit Predictions"></p>
   </form>

{% endblock %}

models

# Fixture prediction table
class Fixture_prediction(db.Model):
    __tablename__ = "fixture_prediction"
    id = db.Column('fixture_prediction_id',db.Integer, primary_key = True)
    fixture_id = db.Column('fixture_id',db.Integer, db.ForeignKey('fixture.fixture_id'))
    user_id = db.Column('user_id',db.Integer, db.ForeignKey('user.user_id'))
    home_score = db.Column('home_score',db.Integer)
    away_score = db.Column('away_score',db.Integer)

回答1:


Your suspicion is correct. You are going to want to pass in request.form to your PredictionListForm:

form = PredictionListForm(request.form, data=MultiDict(data))

WTForms checks inside of request.form first and only if it doesn't find data in it does it fall back on the data keyword argument. If you don't pass in request.form WTForms has no request to pull from, so it will pull data from the only source it has - the data param you passed it with the old data.



来源:https://stackoverflow.com/questions/24130140/how-do-you-store-the-request-form-to-db-through-wtforms-or-error-in-sqlalchemy-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!