0

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

suren
  • 7,817
  • 1
  • 30
  • 51
  • Possible duplicate of [How can I pass arguments into redirect(url\_for()) of Flask?](https://stackoverflow.com/questions/26954122/how-can-i-pass-arguments-into-redirecturl-for-of-flask) – CaptainDaVinci Nov 07 '19 at 18:32

1 Answers1

1

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'))
CaptainDaVinci
  • 975
  • 7
  • 23
  • thank you! I had checked this answer: https://stackoverflow.com/questions/17057191/redirect-while-passing-arguments, which didn't work. – suren Nov 07 '19 at 18:42