简体   繁体   中英

passing variable from flask to javascript [flask]

hi i am creating a captive portal with python and flask (i am inexperienced). i am trying to pass the primary key id of my table from app.py in my index.html code in order to use the id for cookies. how do i integrate id from app.py to index.html with javascript? thanks

my code:

#funzione per registrare un utente
@app.route('/registrautenti',methods=['POST'])
def registrautenti():
    nome = request.form['nome']
    cognome = request.form['cognome']
    email = request.form['email']
    numtelefono = request.form['numtelefono']            
    cursor = cnxn.cursor()
    cursor.execute("""INSERT INTO inserimentoutenti(Nome,Cognome,Email,Numero_Telefono) VALUES (?,?,?,?)""",(nome,cognome,email,numtelefono))
    cnxn.commit()
    cursor.execute("""SELECT @@IDENTITY AS ID;""")
    print('Id è:{}'.format(cursor.fetchone()[0]))
    cursor.close()
    cnxn.close()
    return render_template('userHome.html')

If you're using the UserMixin class from Flask, you might be able to use the current_user object, which has the get_id() function.

Eg: current_user.get_id()

This way you can pass it to your template, put in a hidden element, something like:

Link your javascript to your file and use the JS function document.getElementById('user-id')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM