Saturday, 15 February 2014

python - Handle data between forms -



python - Handle data between forms -

if have registration 3 steps, utilize 3 forms.

something this, demonstrate:

@app.route('/form/step1', methods=['get', 'post']) def form_step1(): form = form_step_1(request.form) ...validate()... homecoming render_template('register.html', form=form) @app.route('/form/step2', methods=['get', 'post']) def form_step2(): form = form_step_2(request.form) ...validate()... homecoming render_template('register.html', form=form) @app.route('/form/step3', methods=['get', 'post']) def form_step3(): form = form_step_3(request.form) ...validate()... homecoming render_template('register.html', form=form)

what right way handle info between these 3 steps? info should committed database @ end of step 3. action between forms should populate 1 time again previous form.

use sessions purpose seems bad.

i suggest using session object pass info 1 form another. if have little amount of info can away using cookie implementation flask has. otherwise, you can override default sessions object store sessions info server side using redis. lets utilize session objects without paying cost of storing lots of info in cookies. means can

@app.route('/form/step1', methods=['get', 'post']) def form_step1(): form1 = form_step_1(request.post) user_id = current_user.user_id # if you're using flask-login ...validate()... # dictionary holds form1, form2, etch form_data = {"form1": form1, "form2": none, "form3"=none} flask.session[user_id] = form_data redirct_to(url_for("form_step2")) homecoming render_template('register.html', {'form':form1}) @app.route('/form/step2', methods=['get', 'post']) def form_step2(): form1 = session[user_id][form1] # simpler way passing whole form info # want reply i'm specifying whole form. form = form_step_2(form1) user_id = current_user.user_id # if you're using flask-login ...validate()... # dictionary holds form1, form2, etch flask.session[user_id]["form2"] = form2 redirct_to(url_for("form_step3")) homecoming render_template('register.html', form=form)

python forms flask flask-wtforms

No comments:

Post a Comment