validate_on_submit无法使用WTF表单



当我在页面上注册时,我看到这些值被插入数据库,它将我重定向到代码中提到的登录功能,但我不明白为什么每次我尝试登录时,登录页面本身都会被重定向,相反,它应该被重定向到聊天功能。

我看到索引功能正在清楚地工作

并且这些值被插入数据库

但当我尝试登录时,它没有被重定向到聊天页面

当我打印";login_form.validate_on_submit";,结果总是false,所以我可以看到validate_on_submit函数有问题,但我不知道它是什么。

这是我在应用程序中的登录功能.py:

@app.route('/login',methods=["GET","POST"])
def login():
login_form=LoginForm()
print(login_form)
if login_form.validate_on_submit():
user_object=User.query.filter_by(username=login_form.username.data)
login_user(user_object)
return redirect(url_for("chat"))
return render_template("login.html", form=login_form)

这是WTForm_Fields.py:

def invalid_credentials(form, field):
""" Username and password checker """
password = field.data
username = form.username.data
# Check username is invalid
user_data = User.query.filter_by(username=username).first()
print(user_data)
if user_data is None:
raise ValidationError("Username or password is incorrect")
# Check password in invalid
elif not pbkdf2_sha256.verify(password, user_data.hashed_pswd):
raise ValidationError("Username or password is incorrect")
class LoginForm(FlaskForm):
"""login form"""
username=StringField('username_label',validators=[InputRequired(message="username required")])
password=PasswordField('password_label',validators=[InputRequired(message="Password Required"),invalid_credentials])

这是login.html:

{% from 'form_helper.html' import displayField %}
{% extends 'prelogin-template.html'%}
{%block title%}Login{%endblock%}
{%block content%}
<h2>Login now!</h2>
<p>Enter your username/Password to start!!</p>
<form action="{{ url_for('login') }}",method='POST'>
{{displayField(form.username,"Username",autocomplete='off',autofocus=true)}}
{{displayField(form.password,"Password")}}
<div class="form-group">
<input type="submit" value="Login" class="btn btn-warning">
</div>
{{ form.crsf_token }}
</form>
{%endblock%}

这是prelogin-template.html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>{%block title%}{%endblock%}-Let's Chat </title>
</head>
<body>

{% with messages=get_flashed_messages(with_categories=true) %}
{% if messages %}    
Category:{{ messages[0][0] }}
{{messages[0][1]}}
{% endif %}
{% endwith %}
{%block content%}

{%endblock%}
</body>
</html>

这是form_help.html:

{% macro displayField(fieldName,placeholderValue) %}
<div class='form-group'>
{{fieldName(class="form_control",placeholder=placeholderValue,**kwargs)}}
<ul class="formError">
{% for error in fieldName.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endmacro %}

在我看来,您的form_help.html、prelogin-template.html、login.html和WTForm_Fields.py文件中没有问题。但是,您的登录功能中存在问题。我认为数据库查询错误已经发生。一个建议是简单地在用户对象的末尾添加.first((。当您提供flask_sqlalchemy.BaseQuery对象作为参数时,flask_login有时不会让用户登录。

请尝试在应用程序内的登录功能中执行此操作。py:

@app.route('/login',methods=["GET","POST"])
def login():
login_form=LoginForm()
print(login_form)
if login_form.validate_on_submit():
user_object=User.query.filter_by(username=login_form.username.data).first() #<--add here
login_user(user_object)
return redirect(url_for("chat"))
return render_template("login.html", form=login_form)

这应该会让用户顺利登录。

最新更新