我在Flask中进行表单验证时遇到问题。在login.html中,我有:
{% extends "base.html" %}
{% block content %}
<center>Sign In</center>
<form action="/login" method="post" >
<div class="login">
<input type="text" placeholder="Username" id="username" name="username">
<input type="password" placeholder="password" id="password" name="password">
<input type="submit" value="Sign In">
</div>
<div class="shadow"></div>
</form>
{% endblock %}
在routes.py中,我有:
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
if request.method == 'POST'and form.validate():
if(request.form["username"] is None or request.form["password"] is None):
flash("fill inputs")
else:
user = User.query.filter_by(username=request.form["username"]).first()
if user is None or not user.check_password(request.form["password"]):
flash('Invalid username or password')
return redirect(url_for('login'))
login_user(user)
userCookie = request.form['username']
resp = make_response(render_template('index.html'))
resp.set_cookie('user', userCookie)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index')
return resp
return redirect(next_page)
return render_template('login.html', title='Sign In')
不幸的是,单击登录后,即使我输入正确的用户名和密码也没有任何反应。在我使用WTForms之前,一切都很好,但是我无法添加CSS来修改表单,并且在更改表单之后,验证文档就可以正常工作。有人可以帮我解决这个问题吗?