python - How can I create a form from a list of models using WTForms? -


i have list of prediction models. want bind them form , allow use post back. how can structure form post associates home/away score prediction model's id field each item bind form?

view

@app.route('/predictor/',methods=['get','post']) @login_required def predictions():         user_id = g.user.id     prediction= # retrieve prediction     if request.method == 'post':         if form.validate() == false:             flash('a score missing, please fill in predictions')             render_template('predictor.html', prediction=prediction, form=form)         else:             pred in prediction:                 # store prediction             flash('prediction added')             return redirect(url_for("predictions"))         # display current predictions     elif request.method == 'get':         return render_template('predictor.html', prediction=prediction, form=form) 

form

class predictionform(wtform):     id = fields.integerfield(validators=[validators.required()], widget=hiddeninput())     home_score = fields.textfield(validators=[validators.required()])     away_score = fields.textfield(validators=[validators.required()]) 

template

  <form action="" method="post">     {{form.hidden_tag()}}     <table>         {% pred in prediction %}         <tr>             <td>{{pred.id}}</td>             <td>{{form.home_score(size=1)}}</td>             <td>{{form.away_score(size=1)}}</td>                        </tr>         {% endfor %}     </table>     <p><input type="submit" value="submit predictions"></p>    </form> 

i unable data bind correctly on post. required validators continually fail because post data missing required fields.

you need subform bind items in list of predictions:

the form have described allow submit single prediction. there seems discrepancy because bind iterable of predictions , appear want home , away prediction each. in fact stands never post id field. cause fail form validation. think want list of subforms. so:

# flask's form inherits wtforms.ext.secureform default # wtform base form.  wtforms import form wtform  # never render form publicly because won't have csrf_token class predictionform(wtform):     id = fields.integerfield(validators=[validators.required()], widget=hiddeninput())     home_score = fields.textfield(validators=[validators.required()])     away_score = fields.textfield(validators=[validators.required()])  class predictionlistform(form):     predictions = fieldlist(formfield(predictionform) 

your view need return along lines of:

predictions = # iterable of predictions database werkzeug.datastructures import multidict data = {'predictions': predictions} form = predictionlistform(data=multidict(data))  return render_template('predictor.html', form=form) 

your form need change more this:

<form action='my-action' method='post'>     {{form.hidden_tag()}}     {{ form.predictions() }} </form> 

now print <ul> <li> per item because thats fieldlist does. i'll leave style , tabular form. might bit tricky not impossible.

on post formdata dictionary home , away score each prediction's id. can bind these predictions sqlalchemy model.

[{'id': 1, 'home':7, 'away':2}, {'id': 2, 'home':3, 'away':12}] 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -