Python Forum
Flask-WTF - FlaskForm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask-WTF - FlaskForm
#1
Hi all Smile
I am learning Flask and I have trouble understanding FlaskForm from Flask-WTF.
Code below.

hello.py
class NameForm(FlaskForm):
    name = StringField('What is your name?', validators=[DataRequired()])
    submit = SubmitField('Submit')

@app.route('/', methods=['GET', 'POST'])
def index():
    name = None
    form = NameForm()
    if form.validate_on_submit():
        name = form.name.data
    return render_template('index.html', form=form, name=name)
index.html
{% import "bootstrap/wtf.html" as wtf %}
{% block page_content %}
<div class="page-header">
    <h1>Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1>
</div>
{{ wtf.quick_form(form) }}
{% endblock %}
This simple application get name from the user and displays a personalized message. Suppose I have entered my name and click submit button. How it's possible that entered name is recived from form object (instance of NameForm) and send to render
name = form.name.data
when before this, new NameForm instance is assigned to the form variable?
form = NameForm()
Reply
#2
The function validate_on_submit populates the form properties based on the current request data and validates the data values.

You can look in your virtual environment folder for the module containing the class definition of validate_on_submit function.
Reply
#3
check this out: https://blog.miguelgrinberg.com/post/the...-web-forms
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020