以下是这个问题: SQLalchemy/wtforms update issue - 400 bad request 我有一个烧瓶框架
问题
当我提交表单时,flash消息出现说预测添加虽然当我查询db时没有任何改变?谁能找到我出错的地方?
我想要实现的目标
用户可以查看他们的预测,对当前预测进行更改。如果没有新的预测,那么他们可以提交新的预测。
视图
# 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)
思想 我有一种感觉,下面只是提交已经存在的东西而不是表格请求......
.update({'home_score':prediction.home_score.data\
,'away_score':prediction.away_score.data})
模板
{% 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 %}
模板 - 替代
{% 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 %}
模型
# 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)
答案 0 :(得分:1)
你的怀疑是正确的。您希望将request.form
传递给PredictionListForm
:
form = PredictionListForm(request.form, data=MultiDict(data))
WTForms首先检查request.form
内部,并且只有在它找不到数据时才会检查data
关键字参数。如果你没有传入request.form
WTForms没有请求撤消,那么它将从它拥有的唯一来源提取数据 - 你通过旧传递的data
参数数据。