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 Login View

Drew Butcher
Drew Butcher
33,160 Points

login on username or password

I am trying to make the login valid on a username or a password. So I tried the following adjustments; however, without success.

In forms.py I have:

class LoginForm(Form):
     username_or_email = StringField('Username or Email', validators=[DataRequired()])
     password = PasswordField('Password', validators=[DataRequired()])

Then in my app.py to get the user I have:

@app.route('/login', methods=('GET', 'POST'))
def login():
     form = forms.LoginForm()
     if form.validate_on_submit():
          try:
               user = models.User.select().where(models.User.email == form.username_or_email.data | models.User.username == form.username_or_email.data).get()
....

1 Answer

Peewee's Querying documentation states that:

...the actual comparisons are wrapped in parentheses. Python’s operator precedence necessitates that comparisons be wrapped in parentheses.

So maybe try:

@app.route('/login', methods=('GET', 'POST'))
def login():
    form = forms.LoginForm()
    if form.validate_on_submit():
        try:
            user = models.User.select().where(
                (models.User.email == form.username_or_email.data) |
                (models.User.username == form.username_or_email.data)).get()
Drew Butcher
Drew Butcher
33,160 Points

You are awesome! Thank you so much :)

You're very welcome!

Good thinking with doing things a little different from just following the videos. Makes for a great learning experience!

Perhaps when you finish you should post your finished code on the forums to share with everyone. :)