Skip to content
Advertisement

How to pass values to return redirect(url_for()) from one method to another in flask

  • I have a method in this I am extracting email id and mobile number from the database.
  • I am using POSTGRES as database and using python language.I need to pass the value to the second method for the OTP purpose but I am running into an error.
@app.route("/", methods=['GET', 'POST'])
def userlogin():
            sql_mobile_query = """SELECT users.phone FROM users WHERE users.email = %s"""
            cursor.execute(sql_mobile_query, record_to_search)
            mobile = cursor.fetchone()
            mobile_new = mobile[0]
            sql_email_query = """SELECT users.email FROM users WHERE users.email = %s"""
            cursor.execute(sql_email_query, record_to_search)
            email = cursor.fetchone()
            email_new = email[0]
            connection.commit()
            if(role == ('Admin',) and eotp == ('False',) and motp == ('False')):
                return "No need of OTP"
            else:
                return redirect(url_for('otp', email=email_new , mobile=mobile_new))
            connection.commit()
            print("Logged in sucessfully")
  • I have another method I need to access the email id and mobile number in this method that is passed from the above userlogin() method
@app.route("/otp/<email>/<mobile>", methods=['GET', 'POST'])
def otp(email, mobile):
    email = email
    mobile = mobile
    print(email, mobile)
    return render_template("otp_screen.html")
  • Right now I am getting this error during execution enter image description here

Advertisement

Answer

Solved. I just don’t have to assign new variable in this method @app.route("/otp/<email>/<mobile>", methods=['GET', 'POST'])

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement