Create a forms in flask

Published by Rajendar PY on

FLask :

FLask is a great microframework to start when you are learning python web development. as compare to Django flask gives us full freedom to write core python code in flask style. Most Awesome part of flask is Flask-WTF. Here is the link for official Flask-WTF documentation. So here is the process to implement the Flask-WTF forms.

Setup:

Download the Flask-WTF library

$ pip3 install Flask-WTF

Then we need to create a file forms.py

$ touch forms.py

Now import the Library to our file.

1 from flask_wtf import FlaskForm
2
3


Fask-WTF provides lots of imports when it comes to forms, from validators to fields there is a lot more.

Here are the modules of WTF that I have used

1 from wtforms import StringField, PasswordField, SubmitField, BooleanField
2 from wtforms.validators import DataRequired, Email, EqualTo, Length

Fields are imports like StringField, PasswordField, and etc. These fields determine the type of field you want like you cannot store a password in a StringField cause that will display the password when you will type in the browser.

Validators:

Validators are those parameters that make your forms valid on a certain condition. For ex username must be more than 4 letters and less than 10 letters, Password must be case sensitive so, validators are Datarequired, Email, EqualTO, Length, etc.

Putting them all together in code:

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError

Here are the imports. Now the Class part is here

class RegistrationForm(FlaskForm):
username = StringField('username', validators=[DataRequired(), Length(min=3, max=10)])
email = StringField('email', validators=[DataRequired(), Email()])
password = PasswordField('password', validators=[DataRequired(), Length(min=5, max=20)])
confirm_password = PasswordField("confirm_password",
validators=[DataRequired(), Length(min=5, max=20), EqualTo('password')])
submit = SubmitField('sign up')

In the above code snippet on the very top, this is my form class Registration, then FlaskForm is the perimeter which shows that this is a FLaskForm. After that username, email, password, confirm_password, submit . are the variables that will store the field information. DataRequire(), Length(), EqualTo() are the validators as python is so easy to understand that we can easily see that what these validators do and why they are being used.

Datarequired() is used to check field must not be an empty field.
Length() define the minimum and the maximum number of characters user can give in the form.
EqualTo() is used where you want some field equal to another field, in our case, it is the confirm_field which must be equal to the password field.

So there you have it!!! A simple form in flask python.

Stay tuned and register for more good blogs and information regarding python, PHP, AI, Machine Learning, etc !!!


0 Comments

Leave a Reply