所以我只是想让“提交”按钮正常工作。 正常工作意味着获取用户输入的电子邮件和密码以直接登录到我的登录名。
当前,它仅重定向到index.html,但我希望它以重定向到配置文件或错误的结果来实现。
这是python部分:
@ app.route(“ / login”,Methods = [“ GET”,“ POST”])
def login():
"""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)
这是登录表单
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>
答案 0 :(得分:0)
我发现您的代码中只有一个问题:
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)