我正在使用Flask-WTF将数据提交到表单,但是在提交表单时我无法获得任何数据。这是我的LoginForm类:
class LoginForm(FlaskForm):
username=StringField('Username')
password=PasswordField('Password')
def validate(self):
check_validate = super(LoginForm, self).validate()
if not check_validate:
return False
logging.error('username is %s'%(self.username)
user=User.query.filter_by(username=self.username.data).first()
if not user:
logging.error('username %s: Invalid username or password'%self.username.data)
self.username.errors.append(
'Invalid username or password'
)
return False
if not user.checkPassword(self.password.data):
logging.error('password %s:Invalid username or password'%self.password.data)
self.username.errors.append(
'Invalid username or password'
)
return False
return True
这是我的表格:
{% extends 'base.html' %}
{% from 'base.html' import input %}
{% block title %}Login{% endblock %}
{% block body %}
<form method='POST' action="{{ url_for('main.login')}}">
{{ form.hidden_tag() }}
{{ input(form.username, 'Username') }}
{{ input(form.password, 'Password') }}
<input class='btn-primary' type='submit' value='Login'>
</form>
{% endblock %}
输入文件是包含的jinaja宏
{% macro input(name, label, value='', type='text') %}
<div class="form-group">
<label for='{{ name }}'>{{ label }}</label>
<input type='{{ type }}' name='{{ name }}' value='{{ value | escape }}' class='form-control'>
</div>
{% endmacro %}
所以当我以这种形式输入一些数据并提交时,我总是看到
ERROR:root:username是
似乎没有数据传递给LoginForm
我该怎么办?
@main_blueprint.route('/login', methods=('GET','POST'))
def login():
form = LoginForm()
if form.validate_on_submit():
flash("Your have been logged in.", category="success")
user = User.query.filter_by(username=form.username.data).first()
return render_template(
'user.html',
user=user
)
return render_template('login.html', form = form)
main_blueprint = Blueprint(
'main',
__name__,
template_folder='../templates/main'
)
新: 我尝试通过“ GET”方法提交表单,但发现我的用户名和密码已通过,但仍无法从LoginForm中获取它们
127.0.0.1 - - [02/Dec/2018 01:05:18] "GET /login?csrf_token=...input+ id%3D%22username%22+ name%3D%22username%22+ type%3D%22text%22+ value%3D%22%22%3E=mer&%3C input+ id%3D%22password%22+ name%3D%22password%22+ type%3D%22password%22+ value%3D%22%22%3E=dly1234& submit=Login+In HTTP/1.1" 200 -
'mer'和'dly1234'是我输入的用户名和密码。
答案 0 :(得分:1)
问题已解决,我阅读了登录页面的HTML代码,并在最新版本中发现<script type="text/javascript">
function displayForm(c) {
if (c.value == "1") {
document.getElementById("ccformContainer").style.visibility = 'visible';
document.getElementById("paypalformContainer").style.visibility = 'hidden';
} else if (c.value == "2") {
document.getElementById("ccformContainer").style.visibility = 'hidden';
document.getElementById("paypalformContainer").style.visibility = 'visible';
} else {}
}
</script>
<form>
<input value="1" type="radio" name="formselector" onClick="displayForm(this)"></input>show form 1
<br>
<input value="2" type="radio" name="formselector" onClick="displayForm(this)"></input>show form 2</form>
<div style="visibility:hidden; position:relative" id="ccformContainer">
<form id="ccform" class="needs-validation container" >
<hr>
<p>THIS IS FORM 1</p>
<hr>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustom01">form1 box 1</label>
<input type="text" class="form-control" id="validationCustom01" placeholder="form1 box 1" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">form1 box 2</label>
<input type="text" class="form-control" id="validationCustom02" placeholder="form1 box 2" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
</form>
</div>
<div style="visibility:hidden;position:relative;top:-110px;margin-top:-110px" id="paypalformContainer">
<form id="paypalform" class="needs-validation container">
<hr>
<p>THIS IS FORM 2</p>
<hr>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustom01">form2 box 1</label>
<input type="text" class="form-control" id="validationCustom01" placeholder="form1 box 1" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">form2 box 2</label>
<input type="text" class="form-control" id="validationCustom02" placeholder="form1 box 2" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
</form>
</div>
<br>
<div id="float_right">
<input type="submit" name="submit3" value="submit">
</div>
<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
'use strict';
window.addEventListener('load', function() {
var form = document.getElementById('ccform');
form.addEventListener('submit', function(event) {
console.log(form.checkValidity());
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
if (form.checkValidity() === true) {
// console.log(form.checkValidity());
// event.preventDefault();
this.querySelector('.loader-container').style.display = 'block';
// the line below is just for the demo, it stops the form from submitting
// so that you can see it works. Don't use it
// event.preventDefault();
}
}, false);
}, false);
})();
</script>
的格式为:
input
我注意到这个名字很奇怪,所以我回头换了我的神社:
之前:
<div class="form-group">
<label for='<input id="username" name="username" type="text" value="">'>Username</label>
<input type='text' name='<input id="username" name="username" type="text" value="">' value='' class='form-control'>
</div>
现在:
{{ input(form.username, 'Username') }}
问题结束。
感谢@ kosist,Mega的教程为我提供了很多帮助,他的实现方法启发了我比较最终HTML代码之间的区别。