How do you access query parameters or the query string in Flask routes? It"s not obvious from the Flask documentation.
The example route
/data below illustrates the context that I would like to access that data. If someone requests something like
example.com/data?abc=123, I would like access to the string
?abc=123 or to be able to retrieve the value of parameters like
@app.route("/data") def data(): # query_string = ??? return render_template("data.html")
from flask import request @app.route("/data") def data(): # here we want to get the value of user (i.e. ?user=some-value) user = request.args.get("user")
The full URL is available as
request.url, and the query string is available as
Here"s an example:
from flask import request @app.route("/adhoc_test/") def adhoc_test(): return request.query_string
To access an individual known param passed in the query string, you can use
request.args.get("param"). This is the "right" way to do it, as far as I know.
ETA: Before you go further, you should ask yourself why you want the query string. I"ve never had to pull in the raw string - Flask has mechanisms for accessing it in an abstracted way. You should use those unless you have a compelling reason not to.
Pandas for Everyone: Python Data Analysis (Addison-Wesley Data & Analytics Series), 1st Edition. Pandas for Everyone brings together the practical knowledge and insights you need to solve real-worl...
A Practical Approach to Computer Algorithms Using Python® and C# Rod Stephens started out as a mathematician, but while studying at MIT, he discovered how much fun algorithms are. He took every al...
Learning to code is tremendous fun as you can get instant results, no matter how much more you have to learn. In fact, it’s such fun creating games and programs that it feels effortless once you’r...
This book introduces machine learning methods in finance. It features a unified treatment of machine learn...