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 Macros

Dan Garrison
Dan Garrison
22,457 Points

Flask - Build a Social Network - Form validation is not working.

Here is a link to a snapshot of my workspace: https://w.trhou.se/uwrdy4qmdj

I'm running into some issues with the form validation. When I leave the fields blank and click submit, it should tell me that the data is required, but it's not. I've gone over my code several times and I'm not seeing anything wrong with it. Thoughts?

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Do you have a form that's missing method="POST"?

Dan Garrison
Dan Garrison
22,457 Points

That was it! On the register.html file, I was missing the = between method and "POST". Thanks!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I don't see anything obviously wrong either.

If you put a pdb into your view right before the form.validate_on_submit(), after a post, does anything show in, I think, form.errors?

Dan Garrison
Dan Garrison
22,457 Points

Thanks for looking at this Kenneth. Perhaps I'm doing this wrong, but the page won't load with a 502 -Bad Gateway error when I try and insert pdb.

Am I doing this right?

@app.route('/register', methods=('GET', 'POST'))
def register():
    form = forms.RegisterForm()
    import pdb
    pdb.set_trace()
    if form.validate_on_submit():
        flash("Yay, you registered!", "success")
        models.User.create_user(
            username=form.username.data,
            email=form.email.data,
            password=form.password.data
        )
        return redirect(url_for('index'))
    return render_template('register.html', form=form)
Dan Garrison
Dan Garrison
22,457 Points

So I spent some more time on this today. I downloaded a copy of your files and compared it line by line to what I had and I didn't see anything different. Next, I tried copying my code into a brand new workspace and it did the same thing.

One thing I have noticed is that it is putting the csrf_token in my URL and also in the console ever time I press the button, which seems odd. The working code I downloaded from the video and then copied into workspaces doesn't seem to do this. Thoughts?