Python 使用烧瓶进行形式验证,不使用wtforms
我对烧瓶中的表单验证有问题。在login.html中,我有:Python 使用烧瓶进行形式验证,不使用wtforms,python,validation,flask,Python,Validation,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="
{% 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 %}
不幸的是,在单击登录后,即使我输入了正确的用户名和密码,也不会发生任何事情。在我使用WTForms之前,一切都很好,但是我不能添加css来修改表单,在更改表单之后,验证也不能很好地工作。有人能帮我解决这个问题吗?1。你能更具体地说明什么不起作用吗?2.您仍然可以使用wtforms来验证POST数据,即使您没有使用wtforms在模板中呈现表单(但您必须确保输入的名称、类型等匹配)。ATM,即使我在登录时写入正确的数据,Im也不会重定向到主页面,也不会设置cookies。这看起来就像POST请求之后,脚本无法用代码连接我的表单。我将再次尝试使用wtforms并更改表单的布局
@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')