Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Build a Social Network with Flask Takin' Names Flask-WTF Forms

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

1 Question and 1 code problem with my forms.py file

Hello guys.

I am struggling with replicating Kenneth's code by my own in the workspaces, but first, I have a quick doubt that you may solve:

When we create the personalized validators, when we actually call them in the RegisterForm class, why don't we need () parenthesis?

The code problem instead is that the compiler does not care about the validators that I have written, it just reloads the page and present an empty form again.

Here is my code:

from flask_wtf import Form
from wtforms import StringField, PasswordField
from wtforms.validators import (DataRequired, Regexp, ValidationError,
                                Email, Length, EqualTo)

from models import User

def name_exists(form, field):
    if User.select.where(User.username == field.data).exists():
        raise ValidationError('User with that name already exists.')

def email_exists(form, field):
    if User.select.where(User.email == field.data).exists():
        raise ValidationError('User with that email already exists.')

class RegisterForm(Form):
    username = StringField(
                message=("Username should be one word, letters, "
                        "numbers and underscores only.")
    email = StringField(
    password = PasswordField(
            EqualTo('password2', message='Password must match')
    password2 = PasswordField(
        'Confirm password',
        validators=[ DataRequired()]

I keep looking at it and can't find what the problem may be.

I have taken for granted that the problem must be in this file, but I may be wrong..

Any ideas?



4 Answers

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

As you can see in the docs, so long as our validator just needs access to the content of the field or form, we don't have to include the parentheses to call it. It's basically just like when we used datetime.datetime.now as a default value to a model field; Python will handle calling it on its own.

You're saying that your validators aren't being used? Or, at the very least, the messages aren't showing up?

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Thanks! I understand the question part.

For the code part: Yes, after correcting a couple of mistakes here and there I have managed to reach the register page and the form shows up as in the video.

If I do not touch it and just press submit though, it looks like the page loads again and no error message is displayed to the user.

The successful registration message does not appear as well.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Omg I found out.

After checking forms.py for ages I presumed that file was ok and checked the other files.

Gladly, the problem was in the first one I checked: it turned out I had a typo in method="POST" in register.html




Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Oh, typos are the best ;)

Glad you figured it out

I spent a whole day trying to figure out what my problem is. I googled, stack_overflowed and was about to start asking people in the street, yet after reading this post I got inspired to check my code one more time. Guess what? I forgot to close the '[]' thing in the regular expression in forms.py and it resulted into the whole system to lay down spawning ridiculous errors in the stack trace (I am currently using PyCharm). So, thanks a lot. And for those who struggle - check your code. Probably, you mistyped something. The sad thing is that 'probably' is equal to 101% =\