1
votes

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?

3
Have you looked at form.errors after calling validate_on_submit? - dirn
I printed out form.errors but nothing appeared :\ - phouse512
In your example you print it out before you actually validate the form. It won't have any errors until you try to validate it. - dirn

3 Answers

2
votes

form = LoginForm()

and can optimize the code as follows:

form = LoginForm() if request.method == 'POST' else LoginForm(request.args)
if form.validate_on_submit():
    if login_user(form.user, remember=form.remember_me.data) is True:
        flash(_('Logged in successfully.'))
    return redirect(form.next.data or url_for('index'))
return render_template('users/login.html', form=form)
1
votes

Don't initialize LoginForm class with request.form.

form = LoginForm()
0
votes

Make sure you place the csrf_token inside the form if you're using flask-WTF:

<form method="POST" action="<page for action>">
    {{ name-of-form.csrf_token }}
    ....
    {{ wtf.form_field(name-of-form.name-of-submit-button) }}
</form>