Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTForms in Flask: UndefinedError: 'form' is undefined

I'm new to Flask and WTForms, and I cannot render my HTML page. I've been through the WTforms documentation again and again, but I can't find the source of the error:

jinja2.exceptions.UndefinedError, UndefinedError: 'form' is undefined

My code is below:

forms.py

from flask.ext.wtf import Form
from wtforms import TextField, BooleanField, TextAreaField, StringField, PasswordField, SelectField, validators

class LoginForm(Form):
    username = TextField('Username', [
        validators.Required(), 
        validators.Length(min=4, max=25)
    ])
    password = PasswordField('New Password', [
        validators.Required()
    ])

app.py

from forms import *
from wtforms import Form, fields, BooleanField, TextField, StringField,     PasswordField, validators
from werkzeug.security import generate_password_hash, \
 check_password_hash

@app.route('/login/', methods=['GET', 'POST'])
def login():
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        if load_user(form.username.data) is None:
            flash('Invalid username')
        else: 
            if check_password_hash(cred.password, form.password.data):
                return redirect(url_for('browse'))  
            else: 
                flash('Invalid password')
    return render_template('login.html', title='Login', form=form)

login.html

{% extends "base.html" %}
{% block body %}
    <form action="{{ url_for('login') }}" method="Post">
        <fieldset>
            <div>{{ form.username.label }}: {{ form.username() }}</div>
            <div>{{ form.password.label }}: {{ form.password() }}</div>
            <div class="form-group">
                <button class="btn btn-default" type="submit">
                    <span aria-hidden="true" class="glyphicon glyphicon-log-in"></span>
                    Log In
                </button>
            </div>
        </fieldset>
    </form>    
    <div>
    or <a href="new">Register</a> for an account
    </div>
{% endblock %}
like image 800
smcp Avatar asked Feb 08 '23 09:02

smcp


1 Answers

You still have to use the return statement - for ex:

return render_template('login.html', title='Login', form=form)

in all your if-else branches because, else, the form context variable is not passed to the template. The login function should look like:

@app.route('/login/', methods=['GET', 'POST'])
def login():
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        if load_user(form.username.data) is None:
            flash('Invalid username')
            return render_template('login.html', title='Login', form=form)
    else: 
        if check_password_hash(cred.password, form.password.data):
            return redirect(url_for('browse'))  
        else: 
            flash('Invalid password')
            return render_template('login.html', title='Login', form=form)
    return render_template('login.html', title='Login', form=form)
like image 139
doru Avatar answered Feb 12 '23 12:02

doru