Python – How to access the query string in Flask routes


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, I would like access to the string ?abc=123 or to be able to retrieve the value of parameters like abc.

def data():
    # query_string = ???
    return render_template("data.html")

Best Solution

from flask import request

def data():
    # here we want to get the value of user (i.e. ?user=some-value)
    user = request.args.get('user')