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 View

Build a social network with Flask: Challenge Task 3 of 3

I am trying Challenge Task 3 of 3 which says: {Finally, update the register() view so that the form is validated on submission. If it's valid, use the models.User.new() method to create a new User from the form data and flash the message "Thanks for registering!". You'll need to import flash()}

I keep getting the error: "Bummer! Didn't get a 200 at '/register' " for the Challenge Task 3 of 3. I don't know where I got it wrong in my code. Please help

lunch.py
from flask import (Flask, render_template, flash, g)
from flask.ext.login import LoginManager

import forms
import models

app = Flask(__name__)
app.secret_key = 'this is our super secret key. do not share it with anyone!'
login_manager = LoginManager()
login_manager.init_app(app)




@login_manager.user_loader
def load_user(userid):
    try:
        return models.User.select().where(
            models.User.id == int(userid)
        ).get()
    except models.DoesNotExist:
        return None


@app.before_request
def before_request():
    g.db = models.DATABASE
    g.db.connect()


@app.after_request
def after_request(response):
    g.db.close()
    return response

@app.route("/register", methods=('GET','POST'))
def register():
    form = forms.SignUpForm()
    flash("Thanks for registering!")

    if form.validate_on_submit():

        models.User.new(
          username=form.username.data,
          email=form.email.data,
          password=form.password.data
          )
    return render_template('register.html', form=form)
models.py
import datetime

from flask.ext.bcrypt import generate_password_hash
from flask.ext.login import UserMixin
from peewee import *

DATABASE = SqliteDatabase(':memory:')


class User(Model):
    email = CharField(unique=True)
    password = CharField(max_length=100)
    join_date = DateTimeField(default=datetime.datetime.now)
    bio = CharField(default='')

    class Meta:
        database = DATABASE

    @classmethod
    def new(cls, email, password):
        cls.create(
            email=email,
            password=generate_password_hash(password)
        )


def initialize():
    DATABASE.connect()
    DATABASE.create_tables([User], safe=True)
    DATABASE.close()
forms.py
from flask_wtf import Form
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Email, Length


class SignUpForm(Form):
    email = StringField(validators=[DataRequired(), Email()])
    password = PasswordField(validators=[DataRequired(), Length(min=8)])

3 Answers

Actually, just went through the challenge myself, the issue is actually that there's no username field. Remove the following line and you should be all good to go:

username=form.username.data,

Thanks a million Iain! It worked.

Great!

Please mark my answer as 'best answer' so others know that this question has been solved.

J llama
J llama
12,631 Points

if you look at the forms page where the signup form is created, youll see that it only asks for validation on email and password, which is exactly what youll need to have in your code to pass the tests...... the above codes will work, just get rid of the creation of a username field in the form

So, I think you are only meant to flash the message if the submission is valid... maybe try moving that line to inside the conditional?

I have moved the line for the flash message line to inside the conditional and I'm still getting the error
Bummer! Didn't get a 200 at '/register'.

Here's my code for the register() part:

@app.route("/register", methods=('GET','POST'))
def register():
    form = forms.SignUpForm()
    if form.validate_on_submit():
        models.User.new(
          username=form.username.data,
          email=form.email.data,
          password=form.password.data)
        flash("Thanks for registering!")
    return render_template('register.html', form=form)

Maybe try change all the indentation to the same amount of spaces? 4 is the norm:

@app.route("/register", methods=('GET','POST'))
def register():
    form = forms.SignUpForm()
    if form.validate_on_submit():
        models.User.new(
            username=form.username.data,
            email=form.email.data,
            password=form.password.data)
        flash("Thanks for registering!")
    return render_template('register.html', form=form)