liveBook · Manning

Request.form

As explained in A Pythonic Adventure: From Python basics to a working web app

request.form is Flask's request data mapping for values submitted through an HTML form.

`request.form` is a Flask dictionary-like mapping that holds values submitted from an HTML form. When someone chooses a drink or types a name and clicks Submit, the route handling the POST request can look up each value by the field’s `name`. section 10.0 section 11.0

Start with matching names. A form field such as `<select name="drink">` sends its selected value under the key `drink`, so the Flask code reads it with `request.form['drink']`. A field named `flavor`, `topping`, or `name` works the same way. section 10.0 section 11.0

Read one submitted choice

A route can serve the form on GET and handle its submission on POST: ```python @app.route('/order', methods=('GET', 'POST')) def order(): if request.method == 'POST': drink = request.form['drink'] print('Drink: ', drink) return render_template('forms.html') ``` Import `request` from Flask before using it. The `drink` variable now contains the choice from the form and can be printed or passed to a template. section 10.0

One form can send several values together. Read each key, then either pass the values separately to a confirmation template or gather them into an order dictionary. That dictionary can be added to an orders list and saved as JSON. section 11.0 section 11.0

Collect a complete order

```python if request.method == 'POST': new_order = { 'name': request.form['name'], 'drink': request.form['drink'], 'flavor': request.form['flavor'], 'topping': request.form['topping'] } orders.append(new_order) save_orders(orders, 'orders.json') return render_template('print.html', new_order=new_order) ``` The keys in `request.form[...]` must match the `name` attributes in the HTML inputs and menus. section 11.0 section 11.0

Listing 11.9 Change to dictionary for a new order in first_app.py
@app.route("/order", methods=("GET", "POST"))
def order():
    if request.method == "POST":
        new_order = {"name": request.form["name"],
                     "drink": request.form["drink"],
                     "flavor": request.form["flavor"],
                     "topping": request.form["topping"]
                    }                                  #1
        orders.append(new_order)                       #2
        save_orders(orders, "orders.json")             #3
        return render_template(
            "print.html", new_order=new_order
        )                                              #4
 
    return render_template("forms.html")

Inspect the full route that turns four submitted fields into one order dictionary, saves it, and sends it to the confirmation template. section 11.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Explains direct dictionary lookups for one and several named fields, then applies them in a route and order dictionary.
Examples
Uses a coffee-order form with drink, flavor, topping, and name fields, followed by a rendered order confirmation.
What it assumes
Introduces form submission through a simple named menu while assuming a Flask app, route, and HTML form are available.
What you can do after
Apply it via Flask to retrieve named form values, render an order, and save orders in JSON.
How it connects
Connects HTML forms, POST requests, route methods, dictionaries, templates, JSON, and file persistence.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.