Introduction
Flask is a web application framework written in Python. It is a microframework because it does not need specific tools or libraries to develop the applications. It supports extensions for object-relational mappers, form validation, upload handling, several standard framework-related tools, etc. WTForms is also a built-in module supported by Flask to provide an interactive user interface for an application. Let’s discuss WTForms and how to handle them in this article.
WTForms
Usually, the HTML forms we use are created with a <form> tag to design an interface. The data entered by a user is submitted in the form of an HTTP request message to the server-side script by either the GET or POST method. So because of this, the form elements have to be defined twice, once in HTML and again in the server-side script, and the form elements are challenging to render dynamically. So to overcome these limitations, we use WTForms.
The WTForms is a built-in module provided by Flask to provide an interactive user interface for an application. It is a flexible, form-rendering, and validating library. These forms provide a simple interface, allow us to define form fields in Python script, and render the form elements dynamically using HTML templates. WTForms also support validation for the elements. To use WTForms, we need to install it first.
pip install flask-WTF
The above command installs the Flask-WTF extension and all libraries, modules necessary in your machine. The installed package contains the Form class, a parent class for all the user-defined forms. Let us discuss few standard form fields among them.
| Form Field | Description |
| Text Field |
Represents the text field <input type=”text”> </input> |
| Boolean Field |
Represents checkbox field <input type=”checkbox”> </input> |
| Decimal Field | Textfield used to display decimal numbers |
| Integer Field |
Textfield used to display integers <input type=”number”> </input> |
| Radio Field |
Represents radio button field <input type=”radio”> </input> |
| Select Field |
Represents select form field <select></select> |
| TextArea Field |
Represents text area field <textarea></textarea> |
| Password Field |
Represents password input field <input type=”password”> </input> |
| Submit Field |
Represents submit form element <input type=”submit”> </input> |
Let’s explore how to write the code for WTForms.
from flask_wtf import Form
from wtforms import TextField,SubmitField
from wtforms import validators, ValidationError
class ContactForm(Form):
name = TextField("Candidate Name ",[validators.Required("Please enter your name.")])
email = TextField("Email",[validators.Required("Please enter your email address."),
validators.Email("Please enter your email address.")])
submit = SubmitField("Submit")
We first created a WTForm in the above code by importing forms and required fields. Then, by using the fields we imported, we added name and email fields to the form to get the user's information. These inputs are taken using Textfield and SubmitField. We called the validator to validate the email given by the user.




