Jason B
Jason B

Reputation: 7465

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?

Upvotes: 3

Views: 6231

Answers (1)

dirn
dirn

Reputation: 20739

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')

Upvotes: 5

Related Questions