Home > Software engineering >  Flask redirect and use data in new html
Flask redirect and use data in new html

Time:09-29

I have a website that uses a form to gather data, compute and output some new data on another page. My form and calculations work perfectly, placing all the information needed into a list 't'. However, when I hit submit and move to the next html I can't figure out how to carry the list with me so that I can display the information I want. I found another thread that mentions what I want to do in the answer but haven't been able to figure it out. Any help would be appreciated.

Redirect with data parameter in flask

routes.py

@app.route('/tcalc', methods=["GET", "POST"])
    def tcalc():
        form = TCalcForm()
            if form.validate_on_submit():
                lng1 = form.lng1.data
                qty1 = form.qty1.data
                ...
                t = [t1, t2, t3]
                return redirect("/tdata")
            return render_template('tcalc.html', form=form)

@app.route('/tdata')
def tdata():
    t = t
    return render_template('tdata.html', t=t)

CodePudding user response:

You can use the session module to pass the parameters you want around routes like so:

from flask_session import Session

@app.route('/tcalc', methods=["GET", "POST"])
    def tcalc():
        form = TCalcForm()
            if form.validate_on_submit():
                lng1 = form.lng1.data
                qty1 = form.qty1.data
                ...
                t = [t1, t2, t3]
                session["my_list"] = t
                return redirect("/tdata")
            return render_template('tcalc.html', form=form)

@app.route('/tdata')
def tdata():
    t = session["my_list"]
    return render_template('tdata.html', t=t)

CodePudding user response:

You could use redirect with parameters and use the parameters in the redirected route like this

@app.route('/tcalc', methods=["GET", "POST"])
def tcalc():
    form = TCalcForm()
        if form.validate_on_submit():
            lng1 = form.lng1.data
            qty1 = form.qty1.data
            ...
            t = [t1, t2, t3]
            # Using f-string to pass the list to the next route
            return redirect(f"/tdata?t={t}")
        return render_template('tcalc.html', form=form)

@app.route('/tdata')
def tdata():
    # here request.args.get("t") will get your list as a string
    # and the eval function will convert the string to list
    t = eval(request.args.get("t"))
    return render_template('tdata.html', t=t)

When redirecting to the next route, I have given a URL parameter with the list value. After execution the URL will look something like this .../tdata?t=[t1, t2, t3].

And in the tdata route the variable t is extracted but all the URL parameters are the read as strings so it will return something like "[t1, t2, t3]". And the eval() function will convert that into an actual list. This is one way of doing it. The only problem would be that all the data will be passed through the url, which is not that secure.

There are two options:

  1. Either like the other answers suggested you could use the session to pass values.
  2. Or else encrypt the the data being passed through the urls.
  • Related