/flask-data-querystrings

Examples of flask apps that have query strings

Flask Apps with Query Strings

A set of lessons and examples in which a __ is presented to the user to allow search/filtering by freeform text, radio buttons, and/or checkboxes.

Lessons

Code concepts in general

So far in our Flask apps, we've dealt two kinds of simple Flask apps:

Single-page Flask apps

These's apps consist of a single page of data results:

Multi-page Flask apps

Apps that can generate multiple pages based on named route paths and variables:

URL query strings

The iteration here is not too complicated. Instead of generating variable routes like:

    /senators/state/CA

We'll be specifying variables in the query string; the below key-value pair is roughly the same effect as the path shown above:

   /senators?state=CA        

Using query strings allows for greater flexibility in passing variables:

   /senators?state=CA&sort_by=age

About webforms

How do we pass in those different key-value pairs? In our HTML view, we render a web form that contains input fields for 'state' and 'sort_by' -- or whatever key-value pairs you want.

A web form, in its most basic variation, consists of a <form> tag that:

About the Flask Request object

The Flask documentation has a section on the Request object, which is the data object that the Flask application exposes to a given view function after matching based on an endpoint and query parameters.

For example, if we have an endpoint with a route path of /results and two key-value pairs of:

  • 'id and 42
  • name and mary

The resulting a URL path and query string will be:

  /results?id=42&name=mary

The Flask application will serialize the parameters as part of an object named request (which has nothing to do with the Requests library).

Specifically, the request object will have an attribute named args, which is a dictionary of keyvalue pairs, i.e.:

      {'id': '42', 'name': 'mary'}