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
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.
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.
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.
There are two ways to do this.
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:
That said, this is the more common way to have hidden parameters passed in the web.
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" ...>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With