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|
Represents <input type = 'text'> HTML form element
Represents <input type = 'checkbox'> HTML form element
Textfield for displaying number with decimals
TextField for displaying integer
Represents <input type = 'radio'> HTML form element
Represents select form element
Represents <testarea> html form element
Represents <input type = 'password'> HTML form element
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")