I am working on a small education project for WTForms. I have to validate the email and password and after successful validation, it should point open success.html otherwise it points to denied.html but it remains on login page.I also attached screenshot of login page
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Email, Length
class LoginForm(FlaskForm):
email = StringField(label='Email', validators=[DataRequired(), Email()])
password = PasswordField(label='Password', validators=[DataRequired(), Length(min=8)])
submit = SubmitField(label='Log In')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'qwerty'
@app.route("/")
def home():
return render_template("index.html")
@app.route("/login", methods=["GET", "POST"])
def login():
login_form = LoginForm()
if login_form.validate_on_submit():
if login_form.email.data == "[email protected]" and login_form.password.data == "123456789":
return render_template("success.html")
else:
return render_template("denied.html")
return render_template("login.html", form=login_form)
CodePudding user response:
Read documentation or some tutorial again.
You forgot to use request.POST
from flask import request
@app.route("/login", methods=["GET", "POST"])
def login():
login_form = LoginForm(request.POST)