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 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. :)