On my login page, when a user types an email address that is not registered yet or is in invalid email format it should give him an error message. But when I submit the form I do not get any errors. I tried to print form.errors
and I get an empty dictionary. I used the same logic on registration and it is working there.
forms.py
class LoginForm(FlaskForm):
email = StringField("", validators=[DataRequired(), Email()])
password = PasswordField("", validators=[DataRequired()])
submit = SubmitField("LOG IN")
views.py
@users.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit:
user = User.query.filter_by(email=form.email.data).first()
if user is not None and user.check_password(form.password.data):
login_user(user)
return redirect(url_for("core.home", user=user))
return render_template("login.html", form=form, total_quantity=session["total_quantity"])
login.html
<div id="login-container">
<h5>LOG IN</h5>
<p>Please enter your e-mail and password:</p>
<form method="POST">
{{form.hidden_tag()}}
{{form.email.label()}} {{form.email(placeholder=" Email", id="email")}}<br>
{% for error in form.email.errors %}
<span style="color: #e50000;">{{ error }}</span><br>
{% endfor %}
{{form.password.label()}} {{form.password(placeholder=" Password", id="password")}}<br>
{% for error in form.password.errors %}
<span style="color: #e50000;">{{ error }}</span><br>
{% endfor %}
{{form.submit(id="login")}}
<p>Not a member!
<a class="nav-link d-md-inline" href="{{ url_for('users.registration') }}" id="logreg-link"> REGISTER</a>
</p>
</form>
</div>
CodePudding user response:
form.validate_on_submit() should be a function call. You forgot the parenthesis.