Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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(
        'Username',
        validators=[
            DataRequired(),
            Regexp(
                r'^[a-zA-Z0-9_]+$',
                message=("Username should be one word, letters, "
                        "numbers and underscores only.")
            ),
            name_exists
        ])
    email = StringField(
        'Email',
        validators=[
            DataRequired(),
            Email(),
            email_exists
        ])
    password = PasswordField(
        'Password',
        validators=[
            DataRequired(),
            Length(min=2),
            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?

Ty

Vittorio

4 Answers

Kenneth Love
STAFF
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

Thanks

;)

Vittorio

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% =\