如何使用Python / Flask设置正确提交使用Jinja的wtform结果

时间:2014-06-11 10:14:53

标签: python flask sqlalchemy jinja2 wtforms

我正在尝试做什么

我没有使用form.predictions()而是试图将它分开,所以我可以更好地设计它,虽然当我提交除了第一个预测之外的预测时它不起作用..我不会更新它除了第一个。

我真的不想将它们全部写出来,因此for循环和loop.index

问题

假如如果更新一行分数,它不会更新数据库,虽然我收到一条消息说我有。奇怪的是,只有第1行有效,其他则不行。

视图

# 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)\
                .filter(Fixture_prediction.fixture_id==Fixture.id)\
                .filter(Fixture_prediction.user_id==user_id).all()
    data = {'predictions': prediction}
    form = PredictionListForm(request.form,data=MultiDict(data))
    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'))
    # display current predictions
    elif request.method == 'GET':
        return render_template('predictor.html', form=form)

模板

<form action='' method='post'>
    <table>
     <tr>
      <th>Fixture Id</th>
      <th>Stage</th>
      <th>Home Team</th>
      <th>Home Score</th>
      <th>Away Score</th>
      <th>Away Team</th>
     </tr>
     {%for prediction in form.predictions%}
     <tr>
  <td><input name="predictions-{{loop.index0}}-fixture_id" type="text" value={{prediction.fixture_id.data}}></td>
  <td><input name="predictions-{{loop.index0}}-stage" type="text" value="{{prediction.stage.data}}"></td>
  <td><input name="predictions-{{loop.index0}}-home_team" type="text" value={{prediction.home_team.data}}></td>
  <td><input name="predictions-{{loop.index0}}-home_score" type="text" value={{prediction.home_score.data}}></td>
  <td><input name="predictions-{{loop.index0}}-away_score" type="text" value={{prediction.away_score.data}}></td>
  <td><input name="predictions-{{loop.index0}}-away_team" type="text" value={{prediction.away_team.data}}></td>
 </tr>
 {%endfor%}
</table>
<p><input type="submit" value="Submit Predictions"></p>
</form>

1 个答案:

答案 0 :(得分:3)

我认为问题出现在这段代码中:

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

问题出在for循环的第一次迭代之后,您提交更改,刷新消息并重定向。这三个语句需要之外的for循环中,如下所示:

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