Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - Pass Variables From Redirect to render_template

I have a question regarding redirecting to a function that renders a template. I have two functions:

@user.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        return render_template('register.html')
    email = request.form['email']
    if User.query.filter(User.email == email).first() is not None:
        flash('Account already exists for this email address!')
        return redirect(url_for('user.login'))

and

@user.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    return "Hello"

In the first function, with the line return redirect(url_for('user.login')), I want to pass the email variable with that redirect, so I can have render_template in the second function display that variable on an HTML page. I tried the following:

return redirect(url_for('user.login', defaultEmail=email))

in the first function and

return render_template('login.html', defaultEmail=email))

but it gives me NameError: global name 'email' is not defined. How would I go about doing this?

like image 694
Jason Brooks Avatar asked Nov 22 '25 12:11

Jason Brooks


1 Answers

The url_for should pass email through the query string. It can by accessed by login as part of request.args.

email = request.args.get('defaultEmail')
like image 155
dirn Avatar answered Nov 24 '25 02:11

dirn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!