skameswa skameswa - 1 year ago 312
HTML Question

Flask : how to pass variables between html pages app route

I have a getvlanconfig.html page with a form that collects information like vlanid and vlannetwork. I want to be able to pass that information over to the page showvlanconfig.html that loads when the form is submitted.
I am new to Flask and from whatever lookup i could do, I was unable to find out the best way to do it. Also I have taken a look at sessions and I am not sure if that would be my option here.

from flask import Flask, render_template, url_for, flash, request, redirect

app = Flask(__name__)

def homepage():
return render_template("index.html")

@app.route('/getvlanconfig/', methods=["GET","POST"])
def getvlan():


if request.method == "POST":
getvlanid = request.form['vlanid']
getvlannetwork = request.form['vlannetwork']

return redirect(url_for('showvlan'))
except Exception as e:

return render_template("vlanconfig.html")

@app.route('/showvlanconfig', methods=["GET","POST"])
def showvlan():
getvlanid = ???
getvlannetwork = ??
return render_template("index.html", vlanid = getvlanid, vlannetwork = getvlannetwork)
except Exception as e:

if __name__ == "__main__": = True)

Answer Source

Use the session to store data between requests from the same client.

from flask import session

def getvlan():
    session['vlanid'] = request.form['vlanid']
    return redirect(url_for('showvlan'))

def showvlan():
    vlanid = session['vlanid']

Use a database (or other external, persistent store) to store data in a more generally accessible sense.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download