How do you access the query string in Flask routes?

ghz 3months ago ⋅ 72 views

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 abc.

@app.route("/data")
def data():
    # query_string = ???
    return render_template("data.html")

Answers

In Flask, you can access query parameters from the request object. The request object provides a way to access query parameters, form data, and more. To retrieve query parameters or the query string, you use request.args for query parameters and request.query_string for the raw query string.

Here’s how you can access query parameters and the query string in a Flask route:

Accessing Query Parameters

Query parameters are accessed through the request.args attribute, which is a MultiDict (essentially a dictionary-like object) containing all the query parameters.

Here’s how you can access a specific query parameter:

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/data")
def data():
    # Get a specific query parameter, e.g., 'abc'
    abc_value = request.args.get('abc')
    
    # If you want to get all query parameters as a dictionary
    all_params = request.args.to_dict()
    
    return render_template("data.html", abc_value=abc_value, all_params=all_params)
  • request.args.get('abc') retrieves the value of the query parameter abc. If abc is not present in the query string, it returns None or a default value if provided.
  • request.args.to_dict() converts all query parameters to a regular dictionary.

Accessing the Raw Query String

If you need the raw query string, including the ?, you can use request.query_string. This gives you the query string as a byte string.

Here’s how to access it:

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/data")
def data():
    # Get the raw query string (including '?')
    raw_query_string = request.query_string.decode('utf-8')
    
    return render_template("data.html", raw_query_string=raw_query_string)
  • request.query_string returns the query string as bytes, so you typically decode it to a string using .decode('utf-8').

Summary

  • Query Parameters: Use request.args to access query parameters. Use request.args.get('param_name') for a specific parameter and request.args.to_dict() for all parameters.
  • Raw Query String: Use request.query_string to get the raw query string.

Here is a complete example of a Flask app that demonstrates both methods:

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/data")
def data():
    abc_value = request.args.get('abc')
    all_params = request.args.to_dict()
    raw_query_string = request.query_string.decode('utf-8')
    
    return render_template("data.html", 
                           abc_value=abc_value, 
                           all_params=all_params, 
                           raw_query_string=raw_query_string)

In this example, you can use data.html to display the query parameters and the raw query string as needed.