18

In my website I have urls that will have trailing parameters like:

example.com/magicpage/?p=10&d=somestuff

Is there a way for me to remove these parameters after the request has been processed? So when the user clicks on a link, the parameters are passed, but the visible URL is simply:

example.com/magicpage

My code:

@app.route("/magicpage")
def magicPage():
    #parse parameters and do things
    #finish up
    #remove the trailing parameters in the url
    #return the rendered page
2
  • Is it a page that will possibly be linked to externally or is it part of a web app which requires context (the parameters) to display properly? Commented Nov 4, 2014 at 18:53
  • Requires context to display the proper content. Commented Nov 5, 2014 at 2:04

2 Answers 2

16
+25

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

8 Comments

The funny thing is, I'm actually passing the initial parameters as a POST request, so the parameters are not visible, but to keep the session on the next page (say that you return a result of multiple pages, then my page2 link would have the parameters so that I can continue serving the filtered content). So the parameters are actually embedded in the urls and does not actually result from a form. If I redirect I would lose the parameters as well as the content that I'm serving. :(
@Stupid.Fat.Cat In that case, what you are looking for is the session. It allows you to pass information between requests without needing to use url parameters. flask.pocoo.org/docs/0.10/quickstart/#sessions
@Stupid.Fat.Cat anything else you're looking for in this answer to accept it?
seems like I have to do a lot of refactoring until I get to the point where this is actually implemented. Until then I'm not sure if it'll work so I can't accept it yet :(
Perhaps as a cobblecode alternative to session, if you want to avoid them for some reason (but why would you?), the "parameters" could be embedded into the links of the page you generate after the post-POST redirect. All the warnings about safety and manageability still apply, though, which is why session still sounds like a better idea.
|
2

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

<input type="hidden" ...>

Comments

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.