官术网_书友最值得收藏!

Forms

Flask-WTF extensions provide the ability to develop forms in Flask and render them through Jinja2 templates. Flask-WTF extends the WTForms library, which has standard patterns to design a form.

We need two forms for this, such as SignupForm and LoginForm. The following is the code for creating forms classes.

File—auth/forms.py:

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import Required, Length, Email, EqualTo

class LoginForm(FlaskForm):
email = StringField(
'Email', validators=[Required(), Length(1,64), Email()]
)
password = PasswordField(
'Password', validators=[Required()]
)
submit = SubmitField('Log In')


class SignupForm(FlaskForm):
email = StringField(
'Email', validators=[Required(), Length(1,64), Email()]
)
password = PasswordField(
'Password', validators=[
Required(),
EqualTo('confirm_password', message='Password must match.')]
)
confirm_password = PasswordField(
'Confirm Password', validators=[Required()]
)
submit = SubmitField('Sign up')

Here, we create the forms with some validations. Now, we are going to use these forms in the views section, where we are going to render the templates along with the forms context.

主站蜘蛛池模板: 青浦区| 海阳市| 海口市| 南昌市| 安岳县| 元氏县| 扎赉特旗| 曲阳县| 瓦房店市| 兴安盟| 苏尼特左旗| 宝应县| 临沭县| 河北区| 曲靖市| 五常市| 灵石县| 盐边县| 龙江县| 手游| 巴里| 翼城县| 民和| 杨浦区| 当雄县| 木兰县| 东丰县| 双流县| 阿图什市| 阿拉善左旗| 石楼县| 三门县| 桐城市| 武陟县| 营山县| 峨眉山市| 博乐市| 丹东市| 普宁市| 醴陵市| 麻城市|