liveBook · Manning

Web form

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

An HTML form is a web-page interface containing controls for collecting user input and submitting it to a server.

A web form turns a page into a place to enter information and act on it. A login form, for example, collects an email address and password; its submit button sends the values to a Flask handler as an HTTP POST request. Because people can leave fields empty or enter invalid values, the handler needs more than the raw submission. section 8.2.2

Flask-WTF connects form definitions to rendered HTML controls and submitted values. A LoginForm defines email and password fields with validators, including required input, length limits, and email format. The template renders the form, while the handler receives a form object and can use validated field data. Validation checks the input; checking whether the email and password belong to a user is a separate login step. section 8.2.2

{% macro validation_errors(errors) %}
    {% if errors %}
        {% for error in errors %}
            <div class="text-danger small">{{error}}</div>
        {% endfor %}
    {% endif %}
{% endmacro %}

This Jinja macro displays each validation error beside a field, making failed submissions useful to the person filling out the form. section 8.2.2

The same pattern supports content creation. A post form provides a title field, a content field that can contain Markdown, and separate Submit and Cancel controls. Its handler renders the empty form, redirects on Cancel, and validates on Submit. Only a valid submission becomes a Post model instance that is saved to the database; the handler then redirects to the new post. Field attributes such as autofocus and tabindex can make the form easier to navigate. section 11.5.1 section 11.5.2

Forms also adapt to the task around them:

  • For an update, populate the fields from the existing post, require login, then save valid changes or leave without changes on Cancel. section 11.6.3
  • For a comment, show a text area in a modal window and carry the parent post identifier in a hidden field; Create submits the comment form, while Cancel closes the window. section 11.8.1 section 11.8.2

Keep the responsibilities clear: the form defines inputs and validation, the template presents them and their errors, and the handler decides what a valid submission changes. That separation lets a familiar page control serve login, creation, or editing without putting the whole workflow in the HTML. section 8.2.2 section 11.5.1

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Works through field definitions, validators, rendering, and handler decisions; does not detail browser form internals.
Examples
Shows login fields and validation errors, post creation and updates, and a modal comment form with a hidden parent identifier.
What it assumes
Builds on Flask handlers, Jinja templates, and database models while defining form fields and validators explicitly.
What you can do after
Build it from scratch using Flask-WTF fields and validators, templates, and handlers to process login or content submissions.
How it connects
Connects forms to HTTP POST, input validation, Jinja templates, authentication, SQLAlchemy models, and database sessions.

Also explained in

Compare how 12 books explain web form →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.