0

I have an html form where a user can enter a stock ticker. After submitting the ticker I'd like the app to redirect to a url that has the entered ticker in the url path and returns the ticker name.

Code:

@app.route('/', methods=['GET', 'POST'])
def stockInfo():
    if request.method == "POST":
        ticker = request.form['ticker'].upper()
        return redirect('/<ticker>')
    return render_template('home.html')

@app.route('/<ticker>', methods=['GET', 'POST'])
def success(ticker):
    return '<h1>The ticker' + ticker + 'was entered </h1>'

What I would like to happen is when the user submits the ticker "NFLX" it routes to /NFLX and returns "The ticker NFLX was entered"

1 Answer 1

2

You can do this by using Flask's url_for function:

@app.route('/', methods=['GET', 'POST'])
def stockInfo():
    if request.method == "POST":
        ticker = request.form['ticker'].upper()
        return redirect(url_for('success', ticker=ticker))
    return render_template('home.html')

@app.route('/<ticker>', methods=['GET', 'POST'])
def success(ticker):
    return '<h1>The ticker' + ticker + 'was entered </h1>'
Sign up to request clarification or add additional context in comments.

1 Comment

added url_for to imports and worked perfectly thanks

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.