`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
