WTForms/flaskforms 无法通过提交按钮正确检索数据
WTForms/flaskforms not retrieving data properly via submit button
所以我只是想让提交按钮正常工作。
正常工作意味着将用户输入的电子邮件和密码定向到我的登录名。
目前,它只重定向到 index.html,但我希望它的结果是重定向到配置文件或错误。
这是 python 部分:
@app.route("/login", methods=["GET", "POST"])
定义登录():
"""Log user in if credentials provided are correct."""
form = LoginForm(request.post)
# this is if its POST
if form.validate and request.method == 'POST':
email = request.form['email']
password = request.form['password']
if email == admin@gmail.com" and password == "admin":
return redirect(url_for('/home'))
else:
return redirect(url_for('/error'))
# this is if its GET
#return render_template("index.html", form=form)
这是登录表单
class LoginForm(FlaskForm):
email = StringField('email', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
remember = BooleanField('remember me')
这是 html 部分:
<div class="modal-body">
<form role="form">
<div class="form-group">
<div class="input-group">
<form method="POST" action="{{ url_for('login')}}">
{{ form.csrf }}
<dl style="width: 100%;">
<div class="form-group">
<form role="form">
<div class="form-group">
<div class="input-group">
{{ wtf.form_field(form.email) }}
{{ wtf.form_field(form.password) }}
</div>
</div> <!-- /.form-group -->
</form>
</div> <!-- /.form-group -->
<div style="margin-left: 70%;" class="checkbox">
{{ wtf.form_field(form.remember) }}
</div>
<div class="modal-footer">
<input class="btn btn-lg btn-primary btn-block" style="background-color: #3eb2a0;border-color: #3eb2a0;" type="submit" value="Sign In">
</div>
</dl>
</form>
</div>
</div>
</form>
</div>
我在您的代码中看到的唯一问题是:
if email == admin@gmail.com" and password == "admin":
return redirect(url_for('/home'))
admin@前没有引号gmail.com
两个:
return redirect(url_for('/home'))
正斜杠有原因吗?你试过了吗'home'
编辑:
这是我如何设置与您的相似的视图的示例
@bp.route('/upvote', methods=['GET', 'POST'])
def view():
form = key()
if form.validate_on_submit():
received_key = form.key_code.data
url = form.url.data
username = form.username.data
return redirect(url_for('views.success')) #views is the name of the blueprint the success route is in
return render_template('upvote.html', title='title here', form=form)
form.validate_on_submit():
取代 form.validate 和 form.submit。data/if response.method ='POST'
然后您可以通过 form.variable.data 检索存储在表单中的数据。
检查一下您是否从表单中接收到数据。似乎它可能没有收到 post 请求并跳过你的 "if request.method = 'POST'" 语句下的所有内容。
您的视图如下所示:
@app.route("/login", methods=["GET", "POST"])
def login()
form = LoginForm()
# this is if its POST
if form.validate_on_submit():
email = form.email.data
password = form.password.data
if email == "admin@gmail.com" and password == "admin":
return redirect(url_for('home'))
else:
return redirect(url_for('error'))
#return render_template("index.html", form=form)
所以我只是想让提交按钮正常工作。 正常工作意味着将用户输入的电子邮件和密码定向到我的登录名。
目前,它只重定向到 index.html,但我希望它的结果是重定向到配置文件或错误。
这是 python 部分:
@app.route("/login", methods=["GET", "POST"])
定义登录():
"""Log user in if credentials provided are correct."""
form = LoginForm(request.post)
# this is if its POST
if form.validate and request.method == 'POST':
email = request.form['email']
password = request.form['password']
if email == admin@gmail.com" and password == "admin":
return redirect(url_for('/home'))
else:
return redirect(url_for('/error'))
# this is if its GET
#return render_template("index.html", form=form)
这是登录表单
class LoginForm(FlaskForm):
email = StringField('email', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
remember = BooleanField('remember me')
这是 html 部分:
<div class="modal-body">
<form role="form">
<div class="form-group">
<div class="input-group">
<form method="POST" action="{{ url_for('login')}}">
{{ form.csrf }}
<dl style="width: 100%;">
<div class="form-group">
<form role="form">
<div class="form-group">
<div class="input-group">
{{ wtf.form_field(form.email) }}
{{ wtf.form_field(form.password) }}
</div>
</div> <!-- /.form-group -->
</form>
</div> <!-- /.form-group -->
<div style="margin-left: 70%;" class="checkbox">
{{ wtf.form_field(form.remember) }}
</div>
<div class="modal-footer">
<input class="btn btn-lg btn-primary btn-block" style="background-color: #3eb2a0;border-color: #3eb2a0;" type="submit" value="Sign In">
</div>
</dl>
</form>
</div>
</div>
</form>
</div>
我在您的代码中看到的唯一问题是:
if email == admin@gmail.com" and password == "admin":
return redirect(url_for('/home'))
admin@前没有引号gmail.com
两个:
return redirect(url_for('/home'))
正斜杠有原因吗?你试过了吗'home'
编辑:
这是我如何设置与您的相似的视图的示例
@bp.route('/upvote', methods=['GET', 'POST'])
def view():
form = key()
if form.validate_on_submit():
received_key = form.key_code.data
url = form.url.data
username = form.username.data
return redirect(url_for('views.success')) #views is the name of the blueprint the success route is in
return render_template('upvote.html', title='title here', form=form)
form.validate_on_submit():
取代 form.validate 和 form.submit。data/if response.method ='POST'
然后您可以通过 form.variable.data 检索存储在表单中的数据。
检查一下您是否从表单中接收到数据。似乎它可能没有收到 post 请求并跳过你的 "if request.method = 'POST'" 语句下的所有内容。
您的视图如下所示:
@app.route("/login", methods=["GET", "POST"])
def login()
form = LoginForm()
# this is if its POST
if form.validate_on_submit():
email = form.email.data
password = form.password.data
if email == "admin@gmail.com" and password == "admin":
return redirect(url_for('home'))
else:
return redirect(url_for('error'))
#return render_template("index.html", form=form)