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 Form with Validators

Devin Scheu
Devin Scheu
66,191 Points

Peewee Help

Question: Create a new Form class named SignUpForm. Give it two fields, email and password. email should be a StringField and password should be a PasswordField.

Code:

forms.py
from flask_wtf import Form
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Email, Length

class SignUpForm(Form):
  email = StringField()
  password.email = StringField()
  password = PasswordField()

2 Answers

In Python, all class fields should be declared with the prefix "self". You can initialize class fields (members) through the constructor, which is called init (that's two underscore characters on each side of the word "init"). The constructor should always accept self as a parameter at a minimum. Here's an example:

class Person:
    def __init__(self, first_name, last_name):
        self._first_name = first_name
        self._last_name = last_name

Python has no access limitations (public, private, etc), so I've use the naming convention that uses "_" at the start of member variables that are intended to be private.

If you're not passing in parameters to the constructor, class fields can still be created and initialized in the constructor, for example:

class ItemCounter:
    def __init__(self):
        self._count = 0
        self._item = None  # this could contain a custom type, to be assigned after object creation

When you access member variables within a class you should use self.<variable name>.

Zubeyr Aciksari
Zubeyr Aciksari
3,124 Points

10 Months is a long time and most likely you passed this challenge already, but here is the answer for the ones that may need the answer later on,

from flask_wtf import Form from wtforms import PasswordField, StringField from wtforms.validators import DataRequired, Email, Length

class SignUpForm(Form): email = StringField() password = PasswordField()

Best!