I'm using Flask-WTF to submit a form but I'm running into some difficulties and I can't seem to find any help or other examples. My code is below, but here's what I've found so far. form.validate_on_submit() is not evaluating to true so it is skipping out of that entire block. Next I tried to see what was happening with the request read by python. When I printed out the value (after I initialized LoginForm) of "form.username.data" it printed out "None" so it seems like the form isn't getting created correctly?
I checked the post request that the browser is sending and the necessary information is all visible in that request!
Here's my login form code:
from flask.ext.wtf import Form
from wtforms import TextField, PasswordField
from wtforms.validators import Required
class LoginForm(Form):
username = TextField('username', validators = [Required()])
pin = PasswordField('pin', validators = [Required()])
Here's the route logic:
@app.route('/login', methods = ['GET', 'POST'])
def login():
if g.user is not None and g.user.is_authenticated():
return redirect(url_for('index'))
form = LoginForm(request.form)
print form.errors
if form.validate_on_submit():
if form.username is None or form.username == "":
print "invalid user"
flash('Invalid login. Please try again.')
return redirect(url_for('login'))
user = db.session.query(User).filter_by(username=form.username).filter_by(password=form.pin).first()
print user
if user is None:
flash('User does not exist. Please register.')
return redirect(url_for('welcome'))
login_user(user)
return redirect(url_for('dashboard'))
print "didn't validate"
return render_template('login.html', title = 'Sign In', form = form)
config code:
CSRF_ENABLED = True
And here's the form HTML:
{% extends 'base.html' %}
{% block content %}
<h1>Sign In</h1>
<form action="" method="post" name="login">
{{ form.hidden_tag()}}
<p>
Please enter your username:<br>
{{ form.username }}<br>
</p>
<p>
Please enter your pin:<br>
{{ form.pin }}<br>
</p>
<p><input type="submit" value="Sign In"></p>
</form>
{% endblock %}
I've looked at this for days but I'm at a loss for what to do next. Any pointers or anything obvious I'm missing?
form.errorsafter callingvalidate_on_submit? - dirn