1

I'm using flask_wtf

this is my login form

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, Length


class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired(), Length(8, 128)])
    remember = BooleanField('Remember me')
    submit = SubmitField('Log in')

and this is my login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
<form method="POST" action="">
    {{ form.csrf_token }}
    {{ form.username.label }}<br>{{ form.username }}<br>
    {{ form.password.label }}<br>{{ form.password }}<br>
    {{ form.remember }}{{ form.remember.label }}<br>
    {{ form.submit }}<br>
</form>
</body>
</html>

and my app.py

from flask import Flask, render_template
from forms import LoginForm

app = Flask(__name__)
app.secret_key = 'learnflask'
@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        username = form.username.data
        print(username)
    return render_template('login.html', form=form)


if __name__ == '__main__':
    app.run(debug=True)

my problem is the username does not print . it seems the loginform not be submited,form.validate_on_submit() is false.

1
  • i make a mistake the password min length is 8 Commented May 20, 2019 at 7:53

2 Answers 2

1

For password field the you have set validator to check if the length is between 8 and 128. If the user gives a password with smaller or larger length the validation will be failed. I think this is the reason why form.validate_on_submit() is always false.

I have shown the error message in the template and got the error.

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
    <style>
        .validation_error{
        color: red;
    }
    </style>
</head>
<body>
<form method="POST" action="">
    {{ form.csrf_token }}
    {{ form.username.label }}<br>{{ form.username }}<span class="validation_error">{{ ', '.join(form.username.errors) }}</span><br>
    {{ form.password.label }}<br>{{ form.password }}<span class="validation_error">{{ ', '.join(form.password.errors) }}</span><br>
    {{ form.remember }}{{ form.remember.label }}<span class="validation_error">{{ ', '.join(form.remember.errors) }}</span><br>
    {{ form.submit }}<br>
</form>
</body>
</html>

app.py:

from flask import Flask, render_template
from forms import LoginForm

app = Flask(__name__)
app.secret_key = 'learnflask'
@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        username = form.username.data
        return username
    return render_template('login.html', form=form)

Output:

Valid input:

valid input

Invalid input:

invalid input

Sign up to request clarification or add additional context in comments.

Comments

0

form.validate_on_submit() is false because you haven't added request.form object to LoginForm()

form = LoginForm(request.form)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.