4

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 %}
4
  • Can you fix the indentation in your view? As it is, it's hard to tell what is actually going on. Also it might be helpful to post the full traceback. Commented Dec 4, 2015 at 9:09
  • Actually, I see that that was changed by the previous editor. @KevinGuan, please don't alter people's code unnecessarily, especially to make it wrong where it was previously right. Commented Dec 4, 2015 at 9:10
  • @DanielRoseman: Sorry...don't know what's wrong with the Stack-Exchange-Editor-Toolkit, I was just want to remove the thanks and fix the error message format. Commented Dec 4, 2015 at 9:15
  • 1
    I couldn't reproduce your error. If I commented everything in first condition, it works. For full testing I need to know an implementation of load_user(). And when an error occurs? Or do you have a public git repo with your project? Commented Dec 4, 2015 at 9:41

1 Answer 1

8

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.