[FIXED] AttributeError: 'LoginForm' object has no attribute 'validate_on_sumbit'

Issue

I have done a registration and login page registration page works fine but login page when i Click on create account i get object has no attribute.

application.py

from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField
from wtforms.validators import Length,EqualTo,InputRequired,ValidationError
from models import User
@app.route('/login', methods=['GET', 'POST'])

def login():
    login_form = LoginForm()
    if login_form.validate_on_sumbit():
        return "Logged in, finally!"
    return render_template('login.html', form=login_form)
#wtform_fields.py
class LoginForm(FlaskForm):
    """Login Form """

    username = StringField('username_label',validators=[InputRequired(message="username required")])
    password = PasswordField('password_label',validators=[InputRequired(message="Password required"),invalid_credentials])
    submit   = SubmitField('Login')

login.html

{% from 'form_helper.html' import DisplayField %}

{% extends "prelogin-layout.html" %}

{% block title %} Registration {% endblock %}

{% block content %}

    <h3>Create your account</h3>
    <hr>
    <form action="{{ url_for('index') }}", method="POST" >
       {{DisplayField(form.username, 'Username', autocomplete='off',autofocus=true)}}
       {{DisplayField(form.password, 'Password')}}
       {{DisplayField(form.confirm, 'Confirm Password')}}
        <div class="form-group">
            <input type="submit" value="Create" >
        </div>
        {{form.csrf_token}}
    </form>
   
   
  
{% endblock %}

ErrorLog

in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "C:\Users\Catalyst\Desktop\Python\chatAp\application.py", line 18, in login
    if login_form.validate_on_sumbit():
AttributeError: 'LoginForm' object has no attribute 'validate_on_sumbit'

I’m new at flask can you direct e where I’m mistaken
PS I’m working with flaskwtf V1.0.1

Solution

Change the line that adds submit in login.html from

        <div class="form-group">
            <input type="submit" value="Create" >
        </div>

to

        <div class="form-group">
            {{DisplayField(form.submit, 'Create')}}
        </div>

If you are using Flask WTF all fields in the form must come from the library.

P.S.
I don’t know where you got the confirm variable to check the password from.
It may not work for you either. If you want the user to enter the password twice, read here: wtforms.validators.EqualTo

Answered By – Kamil Pietrzak

Answer Checked By – Terry (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published