2/20/2019 - 11:43 AM

Basic Flask WTF Form

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField,SubmitField

app = Flask(__name__)
# Configure a secret SECRET_KEY
# Has to be here, otherwise the app will not started
app.config['SECRET_KEY'] = 'mysecretkey'

# Now create a WTForm Class
# Lots of fields available:
class InfoForm(FlaskForm):
    This general class gets a lot of form about puppies.
    Mainly a way to go through many of the WTForms Fields.
    breed = StringField('What breed are you?')
    submit = SubmitField('Submit')

@app.route('/', methods=['GET', 'POST'])
def index():
    # Set the breed to a boolean False.
    # So we can use it in an if statement in the html.
    breed = False
    # Create instance of the form.
    form = InfoForm()
    # If the form is valid on submission (we'll talk about validation next)
    if form.validate_on_submit():
        # Grab the data from the breed on the form.
        breed =
        # Reset the form's breed data to be False = ''
    return render_template('00-home.html', form=form, breed=breed)

if __name__ == '__main__':
{% if breed %}
  The breed you entered is {{breed}}.
  You can update it in the form below:
{% else %}
  Please enter your breed in the form below:
{% endif %}

<form method="POST">
    {# This hidden_tag is a CSRF security feature. #}
    {{ form.hidden_tag() }}
    {{ form.breed.label }} {{ form.breed(class='some-css-class') }}
    {{ form.submit() }}