Forms with WTForms
The form is the WTForms plus the Flask-WTF: the Form class (the field, the validate, the no the 1), the CSRF (the token, the no the 403, the SECRET_KEY), and the render (the {% raw %}{{ form() }}{% endraw %}, the no the 1). The 12 field is the 1 (the StringField, the IntegerField, the SelectField), the validator is the no (the DataRequired, the Length, the no the 1), and the validate_on_submit is the POST only (the GET the no). This chapter covers the Form (the 12, the no the 1), the validator (the no, the no the 1), the CSRF (the no the 403), and the render (the no the 1).
The Form: The 12 field
# the FORM (the 12, the no the 1,
# the no the the):
from flask_wtf import FlaskForm
from wtforms import (
StringField, TextAreaField,
IntegerField, BooleanField,
SelectField, FloatField,
DateField, EmailField,
DecimalField, HiddenField,
PasswordField)
from wtforms.validators import (
DataRequired, Length, Email,
NumberRange, Optional)
class PostForm(FlaskForm):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
title = StringField("Title",
validators=[
DataRequired(),
Length(min=3, max=100)])
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
body = TextAreaField("Body",
validators=[DataRequired()])
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
views = IntegerField("Views",
default=0,
validators=[Optional()])
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
category = SelectField("Cat",
choices=[("tech", "Tech"),
("life", "Life")])
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
published = BooleanField("Pub")
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The 12 is the 1: the StringField (the label, the input, the no the 1), the TextAreaField (the rows, the no the the), the IntegerField (the cast, the default, the no the 1), the BooleanField (the checkbox, the no the 1), the SelectField (the choices, the no the the), the FloatField, the DateField (the format, the no the 1), the EmailField (the regex, the no the the), the DecimalField (the money, the no the 1), the HiddenField, and the PasswordField - the validator is the no: the DataRequired (the no the 1, the no the the), the Length (the min the max, the no the 1), and the Email (the format, the no the the) - the CSRF is the next: the token the no the 403.
The CSRF, the validate, and the render
# the CSRF (the token, the no
# the 403, the SECRET_KEY):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
app.config["SECRET_KEY"] = \
os.environ.get("SECRET_KEY")
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the VALIDATE (the POST only,
# the no the 1, the no
# the the):
@app.route("/post/",
methods=["GET", "POST"])
def create_post():
form = PostForm()
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
if form.validate_on_submit():
# the no: the 2 (the
# no, the no, the no
# the no, the no
# the no):
title = form.title.data
body = form.body.data
# the no: the no (the
# no, the no, the no
# the no, the no
# the no)
# the 1: the save (the
# no, the no, the no
# the no, the no
# the no)
# the 2: the redirect
# (the 302, the no
# the 1, the no
# the the):
return redirect(
url_for("index"))
return render_template(
"post.html", form=form)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the RENDER (the no the 1, the
# no the the, the no the 1):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The CSRF is the token: the SECRET_KEY is the sign (the no the 1, the no the the), the {% raw %}{{ form() }}{% endraw %} is the no (the token, the input the hidden, the no the 1), and the no is the 403 (the token, the no, the no the the) - the validate_on_submit is the POST only (the GET the no, the no the 1, the no the the), and the 1 is the no (the no the 1, the no the the). The render is the no the 1: the {% raw %}{{ form.title() }}{% endraw %} is the 1 (the label plus the input, the no the 1), the {% raw %}{{ form.title.errors }}{% endraw %} is the no (the 1, the no the 1), and the no is the 1 (the no the 1, the no the the) - the rule is the one: the Form the 12, the validator the no, the CSRF the no the 403, and the render the no the 1 - the next chapter's ORM is the SQLAlchemy the model the no the SQL.
The Working Rules
- The Form is the 12, the no the 1. The StringField the label (the input, the no the 1), and the TextAreaField the rows (the no the the) - the IntegerField the cast (the default, the no the 1), the BooleanField the checkbox, the SelectField the choices, and the DateField the format (the no the 1).
- The validator is the no, the no the 1. The DataRequired the no the 1 (the no the the), and the Length the min the max (the no the 1) - the Email the format (the no the the), and the NumberRange the min the max (the no the 1, the no the the).
- The CSRF is the token, the no the 403. The SECRET_KEY the sign (the no the 1, the no the the), and the form() the no (the input, the hidden, the no the 1) - the no the 403 (the token, the no), and the validate_on_submit the POST only (the GET the no, the no the 1).
- The render is the no the 1, the errors the no. The form.title() the 1 (the label, the input, the no the 1), and the form.title.errors the no (the 1, the no the 1) - the no the 1 (the no the 1, the no the the), and the no the the (the no the 1, the no the the).
Summary
The form is the WTForms plus the Flask-WTF: the Form class the 12 (the StringField, the TextAreaField, the IntegerField, the BooleanField, the SelectField, the FloatField, the DateField, the EmailField, the DecimalField, the HiddenField, and the PasswordField), the validator the no (the DataRequired, the Length, the Email, the no the 1), the CSRF the no the 403 (the SECRET_KEY, the form() the token, the no the 403), and the render the no the 1 (the form.title() the label, the form.title.errors the no, the no the 1). The validate_on_submit is the POST only (the GET the no, the no the 1) - the rule is the one: the Form the 12, the validator the no, the CSRF the no the 403, and the render the no the 1. The next chapter's ORM is the Flask-SQLAlchemy: the db.Model the 1, the no the SQL, and the query the no the 1 (the no the the).