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


this following on question: sqlalchemy/wtforms update issue - 400 bad request have flask framework

issue

when submit form flash message comes saying prediction added although when query db nothing has changed?? can spot i'm going wrong?

what trying achieve

users able view predictions making changes current predictions. if there no new predictions can submit new predictions.

views

# predictor - user makes predictions , 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 missing, please fill in predictions')             render_template('predictor.html', form=form)         else:             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 have feeling below submitting there in first place , not 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 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 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) 

your suspicion correct. going want pass in request.form predictionlistform:

form = predictionlistform(request.form, data=multidict(data)) 

wtforms checks inside of request.form first , if doesn't find data in fall on data keyword argument. if don't pass in request.form wtforms has no request pull from, pull data source has - data param passed old data.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -