liveBook · Manning

Dropdown

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

A select element is an HTML form control that presents a list of options from which a user can choose.

A select element makes a menu inside an HTML form. Put option elements between its opening and closing tags, and each option becomes a choice the visitor can pick—like Coffee or Decaf. It is a friendly way to let people choose instead of typing everything themselves. section 10.0

Build a coffee-order menu

Start with one drink menu, then grow it a little at a time. Give the select a name such as drink, add a “Choose drink” option, and add choices such as coffee, chocolate, and decaf. Repeat the pattern for flavor and topping menus. section 11.0

The name matters when the form is submitted. A Flask order handler can read request.form["drink"], request.form["flavor"], and request.form["topping"], then pass those selected values to a template that prints the order back to the customer. section 11.0

A small menu checklist:

  • Place the select element inside a form. section 10.0
  • Add one option element for every available choice. section 11.0
  • Use a name so the submitted choice can be retrieved from request.form. section 10.0
  • Try changing and adding options, then open the page in a browser. section 10.0
Listing 11.2 Additional menu options in templates/forms.html
{% extends "base.html" %}
{% block content %}
 
<form action="/order" method="post">
    <select name="drink">
      <option value="">- Choose drink -</option>
      <option value="coffee">Coffee</option>
      <option value="chocolate">Chocolate</option>
      <option value="decaf">Decaf</option>
    </select>
    <select name="flavor">
      <option value="">- Choose flavor -</option>
      <option value="caramel">Caramel</option>
      <option value="vanilla">Vanilla</option>
      <option value="peppermint">Peppermint</option>
      <option value="raspberry">Raspberry</option>
      <option value="plain">Plain</option>
    </select>
    <select name="topping">
      <option value="">- Choose topping -</option>
      <option value="chocolate">Chocolate</option>
      <option value="cinnamon">Cinnamon</option>
      <option value="caramel">Caramel</option>
    </select>
 
    <input type="submit" value="Submit">
</form>
 
{% endblock %}

Inspect three select menus side by side: drink, flavor, and topping. Each follows the same simple pattern, so copying one carefully is a useful first move. section 11.0

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

How deep it goes
Demonstrates select construction, option lists, names, and Flask retrieval without detailing validation or browser internals.
Examples
Uses coffee, chocolate, and decaf menus plus flavor and topping selections in a coffee shop order.
What it assumes
Introduces HTML tag nesting from zero while using Python and Flask request handling in the application.
What you can do after
Build it from scratch as named HTML menus whose submitted choices a Flask handler can display.
How it connects
Connects HTML forms, option elements, form submission, Flask request.form, and templates.

Also explained in

Compare how 10 books explain dropdown →

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.