Reputation: 75
I'm using Flask-WTF to submit data to a form, but I can't get any data when the form is submitted. This is my LoginForm class:
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
And here is my form:
{% 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 %}
the input file is a jinaja macro containing
{% 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 %}
so when I input some data in this form and submit, I always saw
ERROR:root:username is <input id="username" name="username" type="text" value="">
It seems like no data has been pass to LoginForm
What should I do?
@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'
)
new: I tried to submit my form by 'GET' method, and I found my username and password was passed, but I still can't get them in my 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' and 'dly1234' is username and pwd I inputted.
Upvotes: 1
Views: 192
Reputation: 75
The question was resolved, I read the login page's HTML code and found in my latest version the input
in form is:
<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>
I noticed the name in this is strange, So I back to change my jinja:
before:
{{ input(form.username, 'Username') }}
now:
{{ input('username', 'Username') }}
End of the question.
And thanks @kosist, the Mega's tutorial gives me so many helpful, his implement method inspired me to compare the difference between our final HTML code.
Upvotes: 1