Search code examples
python-2.7flaskflask-wtformsflask-appbuilder

How To Pass Form Data from One Page To Another using FlaskApp


i have a form "form.html" where user can fill his information like name, address, mobile no, and image. and i have three html pages "info.html", "success.html" and "failure.html"

i am asking for payment from the user so info.html will show the information filled by user, and later he will make payment if payment is successfull user will be redirected to "success.html", and if payment is failed, user will be redirected to "failure.html"

all three pages are same, just one line is different that is payment status "pending" or "success"

i am able to print user filled information to "info.html" but i want to print same information on success.html or failure.html if needed.

routes.py :

@app.route('/form', methods=['GET', 'POST'])
def form():
  global onlineAppForm, data
  onlineAppForm = RegForm()
  name = request.form['name'].strip()
  father = request.form['father_name'].strip()  
  mother = request.form['mother_name'].strip()
  address1 = request.form['txtAddress1'].strip()
  data = {'name' : name, 'father' : father, 'mother' : mother, 'address1' : address1}
  return render_template("apply1.html", data=data, form=onlineAppForm)

i tryied to do this, but of course it will not work. so how should i do it.

@app.route('/success')
def success():
  return render_template('success.html', data=data, form=onlineAppForm)

Solution

  • You are going to have to setup a database using SQLAlchemy and create a model. A form simply collects the data you need to create the model. Here is a walkthrough of the steps.

    1. Form submitted to endpoint
    2. Create instance of Model and load with form data
    3. Save to db and redirect to success page (pass identifier of object)
    4. query object on success route and pass to template for rendering

    I have written out an example that I believe covers what you need. Didn't feel like writing the templates but I am fairly confident it should work.

    http://hastebin.com/puputugune.py

    also check https://flask-wtf.readthedocs.io/en/latest/ for more help

    EDIT

    on template you can access your model attributes just like you would in your view.

    <!-- success.html -->
    <ul>
        <li>{{ person.id }}</li>
        <li>{{ person.first_name }}</li>
        <li>{{ person.last_name }}</li>
        <li>{{ person.email }}</li>
    </ul>