How to remove parameters from URL in Flask python How to remove parameters from URL in Flask python flask flask

How to remove parameters from URL in Flask python


There are two ways to do this.

Option 1: Use POST parameters rather than GET.

If the parameters are passed by an HTML form, add method=post to the <form> tag, and change your page from:

@app.route("/magicpage")def magicPage():    param1 = request.args.get("param1")    param2 = request.args.get("param2")

to:

@app.route("/magicpage", methods=["POST"])def magicPage():    param1 = request.form.get("param1")    param2 = request.form.get("param2")

Upside is there is no redirection. Downside is if a user tries to refresh the resulting page, they will get the obnoxious browser popup about resubmitting information:

Firefox refresh confirmation of a POST webpage

That said, this is the more common way to have hidden parameters passed in the web.


Option 2: Do a redirect after processing the parameters.

This is a little complicated because since we are redirecting to the same page, we need to check whether they are coming to this page the first time or the second.

The best way to do this is using a post request. This has the advantage of not having the refresh popup, but the disadvantage of not giving you the parameters in the later rendering of the page unless you store them in the session.

@app.route("/magicpage", methods=["GET", "POST"])def magicPage():    if request.method == 'POST':        # process parameters        return redirect(request.path)    if request.method == "GET":        # render page

Alternatively, you could just check for presence of one of the params as your indicator:

@app.route("/magicpage", methods=["GET", "POST"])def magicPage():    if request.form.get("param1", None) is not None:        # process parameters        return redirect(request.path)    else:        # render page


You could use hidden formfields to pass the parameters over multiple pages with POST.

<input type="hidden" ...>