Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove parameters from URL in Flask python

Tags:

python

url

flask

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
like image 499
Stupid.Fat.Cat Avatar asked Oct 10 '14 16:10

Stupid.Fat.Cat


People also ask

How do I hide URL parameters in Flask?

If you'd only want to hide the variable name then you could use converters to create a route like 'users/<str:username>' . Your url would be http://localhost:5000/users/john . Note that hiding the variables completely would mean, that your users would lose the ability to bookmark the page they are on.

How do you take parameters in Flask?

In the first one we would use request. args. get('<argument name>') where request is the instance of the class request imported from Flask. Args is the module under which the module GET is present which will enable the retrieve of the parameters.

How do you pass multiple parameters in a Flask URL?

To add multiple parameters in a Python Flask app route, we can add the URL parameter placeholders into the route string. @app. route('/createcm/<summary>/<change>') def createcm(summary=None, change=None): #... to create the createcm view function by using the app.


2 Answers

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
like image 123
Zags Avatar answered Sep 22 '22 10:09

Zags


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

<input type="hidden" ...>
like image 28
muthan Avatar answered Sep 23 '22 10:09

muthan