0
0
Flaskframework~10 mins

Flask-WTF for form validation - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the FlaskForm base class from Flask-WTF.

Flask
from flask_wtf import [1]
Drag options to blanks, or click blank then click option'
ABaseForm
BFlaskForm
CFormBase
DForm
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Form' instead of 'FlaskForm' causes import errors.
Trying to import 'FormBase' which does not exist in Flask-WTF.
2fill in blank
medium

Complete the code to define a text input field named 'username' using WTForms.

Flask
from wtforms import StringField

class LoginForm(FlaskForm):
    username = [1]('Username')
Drag options to blanks, or click blank then click option'
ATextField
BCharField
CInputField
DStringField
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'TextField' which is deprecated in WTForms.
Using 'CharField' which is not part of WTForms.
3fill in blank
hard

Fix the error in the code to add a required validator to the email field.

Flask
from wtforms.validators import DataRequired, Email

class ContactForm(FlaskForm):
    email = StringField('Email', validators=[[1]])
Drag options to blanks, or click blank then click option'
ADataRequired()
BRequired()
CEmail()
DInputRequired()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Required()' which is deprecated.
Using 'Email()' which only checks email format, not presence.
4fill in blank
hard

Fill both blanks to create a password field with a length validator requiring 6 to 12 characters.

Flask
from wtforms import PasswordField
from wtforms.validators import Length

class RegisterForm(FlaskForm):
    password = PasswordField('Password', validators=[Length(min=[1], max=[2])])
Drag options to blanks, or click blank then click option'
A6
B12
C8
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping min and max values.
Using values outside the required range.
5fill in blank
hard

Fill all three blanks to create a form with a submit button labeled 'Send', a CSRF token, and a text area field named 'message'.

Flask
from flask_wtf import FlaskForm
from wtforms import SubmitField, TextAreaField

class MessageForm(FlaskForm):
    message = [1]('Message')
    submit = [2]('Send')

# CSRF protection is enabled by default with [3]
Drag options to blanks, or click blank then click option'
ATextAreaField
BSubmitField
CFlaskForm
DStringField
Attempts:
3 left
💡 Hint
Common Mistakes
Using StringField instead of TextAreaField for message.
Forgetting that CSRF is enabled by FlaskForm.