尝试使用 Python Flask Wtform 接收数据但无法正常工作
Trying to receive data using Python Flask Wtform but not working
我正在学习Python、Flask 和WTForm。我的目的是在“/login”HTML页面上输入所需的电子邮件和密码,然后单击CommentForm class中定义的“登录”按钮后,页面将被导航到“成功”或“拒绝”页面。但是,代码不起作用。单击按钮后,页面保持静止,而不是导航到另一个页面。下面的代码有什么问题吗?谢谢
在main.py⬇️
class CommentForm(FlaskForm):
email = StringField(label="Email", validators=[Email()])
password = PasswordField(label="Password", validators=[DataRequired(), Length(min=8)])
submit = SubmitField(label="Log In")
recaptcha = RecaptchaField()
@app.route("/login", methods=['GET', 'POST'])
def login():
form = CommentForm(request.form)
if request.method == 'POST' and form.validate():
if form.email.data == "admin@email.com" and form.password.data == "12345678":
return render_template("success.html")
else:
return render_template("denied.html")
return render_template("login.html", form=form)
在login.html⬇️
<form method="post" action="{{ url_for('login') }}">
{{ form.csrf_token }}
{{ form.email.label }}<br>
{{ form.email(rows=1, cols=30) }}
{% for err in form.email.errors %}
<span style="color:red">{{err}}</span>
{% endfor %}<br>
{{ form.password.label }}<br>
{{ form.password(rows=1, cols=30) }}
{% for err in form.password.errors %}
<span style="color:red">{{err}}</span>
{% endfor %}<br>
{{form.submit}}
</form>
我尝试 运行 您的代码(并尝试了一些更改)
Python
@app.route("/login", methods=['GET', 'POST'])
def login():
if request.method == "GET":
form = CommentForm(request.form)
return render_template("login.html", form = form)
elif request.method == 'POST':
form = CommentForm(request.form)
if form.validate_on_submit():
if form.email.data == "admin@email.com" and form.password.data == "12345678":
return "Welcome User"
elif not form.validate_on_submit():
print(form.errors)
return "Hey it is invalid"
return "??"
这导致调用 elif 时出现错误
{'recaptcha': ['The response parameter is missing.']}
解决这个问题
在您的login.html
上添加{{form.recaptcha}}
-
填写表格并获得public和私钥
在你的 flask 应用集上
app.config["RECAPTCHA_PUBLIC_KEY"] = "yourkey"
app.config["RECAPTCHA_PRIVATE_KEY"] = "yourkey"
我正在学习Python、Flask 和WTForm。我的目的是在“/login”HTML页面上输入所需的电子邮件和密码,然后单击CommentForm class中定义的“登录”按钮后,页面将被导航到“成功”或“拒绝”页面。但是,代码不起作用。单击按钮后,页面保持静止,而不是导航到另一个页面。下面的代码有什么问题吗?谢谢
在main.py⬇️
class CommentForm(FlaskForm):
email = StringField(label="Email", validators=[Email()])
password = PasswordField(label="Password", validators=[DataRequired(), Length(min=8)])
submit = SubmitField(label="Log In")
recaptcha = RecaptchaField()
@app.route("/login", methods=['GET', 'POST'])
def login():
form = CommentForm(request.form)
if request.method == 'POST' and form.validate():
if form.email.data == "admin@email.com" and form.password.data == "12345678":
return render_template("success.html")
else:
return render_template("denied.html")
return render_template("login.html", form=form)
在login.html⬇️
<form method="post" action="{{ url_for('login') }}">
{{ form.csrf_token }}
{{ form.email.label }}<br>
{{ form.email(rows=1, cols=30) }}
{% for err in form.email.errors %}
<span style="color:red">{{err}}</span>
{% endfor %}<br>
{{ form.password.label }}<br>
{{ form.password(rows=1, cols=30) }}
{% for err in form.password.errors %}
<span style="color:red">{{err}}</span>
{% endfor %}<br>
{{form.submit}}
</form>
我尝试 运行 您的代码(并尝试了一些更改)
Python
@app.route("/login", methods=['GET', 'POST'])
def login():
if request.method == "GET":
form = CommentForm(request.form)
return render_template("login.html", form = form)
elif request.method == 'POST':
form = CommentForm(request.form)
if form.validate_on_submit():
if form.email.data == "admin@email.com" and form.password.data == "12345678":
return "Welcome User"
elif not form.validate_on_submit():
print(form.errors)
return "Hey it is invalid"
return "??"
这导致调用 elif 时出现错误
{'recaptcha': ['The response parameter is missing.']}
解决这个问题
在您的login.html
上添加{{form.recaptcha}}填写表格并获得public和私钥
在你的 flask 应用集上
app.config["RECAPTCHA_PUBLIC_KEY"] = "yourkey"
app.config["RECAPTCHA_PRIVATE_KEY"] = "yourkey"