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

Didn't find "Thanks for registering!" in the response. task 3 of 3

Where else can I pass in "Thanks for registering!" ? =)

lunch.py
from flask import Flask, render_template, 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)

@app.route('/register', methods=('GET', 'POST'))
def register():
    form = forms.SignUpForm()
    return render_template('register.html', form=form)


@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("Thanks for registering!")
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

Dan Johnson
Dan Johnson
40,533 Points

The after_request function is triggered after every request, so you wouldn't want to put anything dealing with registration in there. Since we're dealing with the registration page we'll put our logic in the corresponding route.

Here's an outline:

@app.route("/register", methods=("GET", "POST"))
def register():
  form = forms.SignUpForm()

  # If the form was filled out properly:
    # Create a new user using the data from the form
    # Flash, "Thanks for registering!" to the screen

  return render_template("register.html", form=form)

You can use the methods/functions: validate_on_submit, new, and flash for this.

thank you Dan, I tried this, but now, "task 1 is no longer passing"

any clue what is causing this? =)

@app.route("/register", methods=("GET", "POST"))
def register():
  form = forms.SignUpForm()

    if form.validate_on_submit():
        models.User.create_user()
        flash("Thanks for registering!")

  return render_template("register.html", form=form)
Dan Johnson
Dan Johnson
40,533 Points

The class method on User is called new, and it'll need the email and password from the form as arguments (e.g. form.email.data).

If you still get, "task 1 is no longer passing" after that make sure you imported render_template and flash from flask.

I had this same issue with it telling me that task 1 is no longer passing. If you cut and paste code into the challenge window it can create weird indentation errors in code that looks fine. I had trouble with this challenge after copy pasting some lines from my app.py file even though the spacing was actually correct. To fix this I just deleted the whitespace at the beggining of the lines and tabbed them back out into place. After that my challenge passed, which confirmed for me that workspaces is kind of buggy. I've had a lot of problems in the past with scripts that seemed to be written right but which wouldn't run. Each time it was because of improperly formatted whitespace (very annoying because you cant see it and the script looks perfectly fine otherwise), something I wasn't thinking to look for.

Aside from that, your code looks fine aside from the missing arguments in the models.User.new() call. It's going to want to accept a password and email from the register form.

models.User.new(email=form.email.data, password=form.password.data)

this will tell the new User instance to take its email and password attributes from the register form. Hope that helps, I was stuck on this one for a little bit, guess that will teach me to pay more attention to whitespace.