WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Flask – WTF


One of the basic parts of a web application is to exhibit a UI for the client. HTML gives a <form> tag, which is utilized to structure an interface. A Form's elements, for example, content information, radio, select and so forth can be utilized appropriately.

Data entered by a client is submitted as Http solicitation message to the server side content by either GET or POST method.

  • The Server side content needs to

  • Another disadvantage of using HTML form is that it is difficult (if not impossible) to render the form elements dynamically. HTML itself provides no way to validate a user’s input.

    the structure elements from http request information. So as a result, structure elements must be characterized twice – once in HTML and again in the server side script.

  • Another disservice of utilizing HTML structure is that it is troublesome (if certainly feasible) to render the structure elements progressively. HTML itself gives no real way to approve a client's input.

This is the place WTForms, an flexible structure, rendering and validation library comes handy. Flask WTF augmentation furnishes a straightforward interface with this WTForms library.

Using Flask-WTF, we can characterize the structure fields in our Python script and render them utilizing a HTML template. It is additionally conceivable to apply validation to the WTF field.

Let us see how this dynamic age of HTML works.

First, Flask-WTF fields should be installed.

pip introduce cup WTF 

The introduced bundle contains a Form class, which must be utilized as a parent for client characterized form.

WTforms bundle contains definitions of different structure fields. Some Standard structure fields are listed below.

Sr.No Standard Form Fields & Description
1

TextField

Represents <input type = 'text'> HTML form element

2

BooleanField

Represents <input type = 'checkbox'> HTML form element

3

DecimalField

Textfield for displaying number with decimals

4

IntegerField

TextField for displaying integer

5

RadioField

Represents <input type = 'radio'> HTML form element

6

SelectField

Represents select form element

7

TextAreaField

Represents <testarea> html form element

8

PasswordField

Represents <input type = 'password'> HTML form element

9

SubmitField

Represents <input type = 'submit'> form element

For example, a form containing a text field can be designed as below −

from flask_wtf import Form
from wtforms import TextField

class ContactForm(Form):
   name = TextField("Name Of Student")