python - WTForms/flaskforms 无法通过提交按钮正确检索数据
问题描述
所以我只是想让提交按钮正常工作。正常工作意味着让用户输入的电子邮件和密码被定向到我的登录名。
目前,它只重定向到 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)
这是登录表格
类登录表单(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'))
正斜杠有原因吗?你试过“家”吗
编辑:
这是我如何设置与您的视图相似的视图的示例
@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 检索存储在表单中的数据。
检查您是否甚至从表单中接收到数据。似乎它可能没有收到发布请求并跳过您的“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)
推荐阅读
- outlook - Outlook.Application .send() 不发送给指定的收件人
- linux - 带有 case 语句和循环的 Shell 脚本不工作
- c# - ValidateCredentials 需要在某些机器上以管理员身份运行
- python - Python:将numpy的补码整数数组转换为二进制补码整数
- javascript - 返回具有相同属性的对象的函数不起作用
- django-forms - Django 表单。覆盖模型字段
- django - 如何使用 Django 表单集实现编辑表单?
- javascript - 允许在文本区域多次插入相同的值
- r - 将置信区间值添加到 GGally:ggpairs 图
- python - JSONField 中的字典查找