1

I have a simple Flask app that contains a few basic views. One of these views is called result. What it does is grabs the URL parameters using GET, then does a bunch of operations using the parameters, and finally renders the template using render_template() and passes it the calculated values.

On rendering, the result URL looks something like this:

http://127.0.0.1:5000/result?s=abcd&t=wxyz

What I want to do is instead of rendering the template under the result view, I'd like to redirect to a new view (let's call it final), passing the calculated values along with the redirect, and render the template from there. Why do all this? Because I'd like the final URL to look like this instead:

http://127.0.0.1:5000/final/abcd

I'm sorry if the title is a bit vague.

1 Answer 1

2

The simplest solution that "abcd" in the final URL can be the actual result:

@app.route("/result")
def calculate_result():
    s, t = request.args.get("s"), request.args.get("t")
    # Calculate result with s and t
    return redirect(url_for(".display_results", result=result))

@app.route("/final/<result>")
def display_results(result):
    return render_template("results.html", result=result)

If it cannot be, then you can use session instead:

@app.route("/result")
def calculate_result():
    s, t = request.args.get("s"), request.args.get("t")
    # Calculate result with s and t
    session["result"] = result
    return redirect(url_for(".display_results", result=result))

@app.route("/final/abcd")
def display_results():
    result = session.get("result")
    return render_template("results.html", result=result)
Sign up to request clarification or add additional context in comments.

2 Comments

I tried using the second method, to no avail. The URL is successfully shown, but the result (calculate_result() stores 3 variables, two of which are dicts) is somehow not carried over in the session. Furthermore, the template is rendered but not rendered. The page contains no CSS, and all of the passed variables are of type None.
@Cyph0n - have you set a SECRET_KEY in your code? The session won't work if you don't have one set.

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.