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

1

1 Answer 1

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'))
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! I had checked this answer: stackoverflow.com/questions/17057191/…, which didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.