Naveera A.
—How do you access the query string in Flask routes?
Query strings are usually made of query parameters. Flask provides an out-of-the-box solution to access parsed query parameters. In most cases, accessing query parameters is enough. But if you want to access the query string itself, you can do so by accessing the query_string
property of the request object.
Adding a query string to the URL is a common way to pass data to a web app. The query string is made up of arbitrary keys and values that are added to the URL after a question mark (?
) character.
For example, in the following URL:
http://example.com?language=python
The query string is language=python
. Inside our app, we refer to ‘language’ as a query parameter and ‘python’ as its value.
Query parameters can be separated by an ampersand (&
) character, like so:
http://example.com/?language=python&framework=flask
Flask provides a request object that contains all sorts of information about the current HTTP request.
For example, you can access the complete URL with the query string by accessing the url
property. Like so:
# import main Flask class and request object from flask import Flask, request # create the Flask app app = Flask(__name__) @app.route('/') def query_example(): print(request.url) # request.url property return 'Query String Example' if __name__ == '__main__': app.run(debug=True, port=5000)
If you run the above code, and send a request to
http://127.0.0.1:5000/?language=python&framework=flask
You will get the following output on your terminal:
http://127.0.0.1:5000/?language=python&framework=flask
If you want just the query string, you can access it like so:
print(request.query_string)
You will get the following output:
b'language=python&framework=flask'
This is the raw value of the query string. The preferred way to access the query parameters is to use the args
property:
print(request.args.get('framework'))
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.