suren
suren

Reputation: 8766

How to pass a variable in `redirect(url_for())`

I am trying to pass a variable while redirecting to another URL, with flask, in python, and am getting name var not defined.

This is what Im doing:

@app.route('/')
def root():
    delay = randint(0, 5)
    time.sleep(delay)
    return redirect(url_for('first_hop', delay=delay))

This seems to be the way to pass the variable, based on other answers.

@app.route('/first_hop')
def first_hop():
    x = delay
    print(delay)
    return redirect(url_for('second_hop'))

But here, I don't know how to get it. Do I have to make the variable global?

The error is below.

NameError: name 'delay' is not defined

Upvotes: 0

Views: 667

Answers (1)

CaptainDaVinci
CaptainDaVinci

Reputation: 1065

In first_hop, the parameters can be obtained using request.args.get('delay').

@app.route('/first_hop')
def first_hop():
    x = request.args.get('delay')
    print(x)
    return redirect(url_for('second_hop'))

Upvotes: 1

Related Questions