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 Tacocat Challenge Tacocat!

tacocat challenge - code wont pass

my code for the tacocat challenge is returning OK after running app_tests.py in workspaces, but i keep getting a "Bummer: Try again!" message in the code challenge section. please help.

tacocat.py
from flask import Flask, redirect, url_for, flash, render_template, g
from flask.ext.bcrypt import check_password_hash
from flask.ext.login import LoginManager, login_required, login_user, logout_user, current_user

import forms, models

app = Flask(__name__)
app.secret_key = "asjkeifm77825fhfh;'-.8ll2-"

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"

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

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

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

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

        models.User.create_user(form.email.data, form.password.data)
        return redirect(url_for("index"))

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

@app.route("/login", methods=("GET", "POST"))
def login():
    form = forms.LoginForm()
    if form.validate_on_submit():
        try:
            user = models.User.get(models.User.email == form.email.data)
        except models.DoesNotExist:
            flash("Email or Password Invalid", "error")
        else:
            if check_password_hash(user.password, form.password.data):
                login_user(user)
                flash("You're successfully logged in", "success")
                return redirect(url_for("index"))
            else:
                flash("Email or Password invalid", "error")

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

@app.route("/logout")
@login_required
def_logout():
    logout_user()
    return redirect(url_for("index"))

@app.route("/taco", methods=("GET", "POST"))
@login_required
def taco():
    form = forms.TacoForm()
    if form.validate_on_submit():
        models.Taco.create(
            protein=form.protein.data,
            shell=form.shell.data,
            cheese=form.cheese.data,
            extras=form.extras.data,
            user=current_user._get_current_object()

        )
        return redirect(url_for("index"))

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

@app.route("/")
def index():
    return render_template("index.html", tacos=models.Taco.select())


if __name__ == "__main__":
    models.initialize()
    #app.run(port=8000, debug=True, host="0.0.0.0")
models.py
from flask.ext.login import UserMixin
from flask.ext.bcrypt import generate_password_hash, check_password_hash
from peewee import *


DATABASE = SqliteDatabase("tacos.db")


class User(UserMixin, Model):
    email = CharField(unique=True)
    password = CharField()

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

        except IntegrityError:
            raise ValueError

    class Meta:
        database = DATABASE


class Taco(Model):
    protein = CharField()
    shell = CharField()
    cheese = BooleanField()
    extras = TextField()
    user = ForeignKeyField(User)

    class Meta:
        database = DATABASE

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


class SignUpForm(Form):
    email = StringField("Email",
                        validators=[
                            DataRequired(),
                            Email()])
    password = PasswordField("Password",
                             validators=[
                                 DataRequired(),
                                 EqualTo("password")])
    password2 = PasswordField("Confirm Password",
                              validators=[
                                  DataRequired()])

class LoginForm(Form):
    email = StringField("Email",
                        validators=[
                            DataRequired(),
                            Email()])
    password = PasswordField("Password",
                             validators=[
                                 DataRequired(),
                                 EqualTo("password")])

class TacoForm(Form):
    protein = StringField("Protein")
    shell = Stringfield("Shell")
    cheese = BooleanField("With Cheese")
    extras = TextAreaField("Extras")
templates/layout.html
<!doctype html>
<html>
  <head>
    <title>Tacocat</title>
    <link rel="stylesheet" href="/static/css/normalize.css">
    <link rel="stylesheet" href="/static/css/skeleton.css">
    <link rel="stylesheet" href="/static/css/tacocat.css">
  </head>
  <body>
    {% with messages=get_flashed_messages() %}
    {% if messages %}
        <div class="messages">
          {% for message in messages %}
          <div class="message">
            {{ message }}
          </div>
          {% endfor %}
        </div>
    {% endif %}
    {% endwith %}

    <div class="container">
      <div class="row">
        <div class="u-full-width">
          <nav class="menu">
              {% if current_user.is_authenticated() %}
                  <a href="{{ url_for("logout") }}">log out</a>
                  <a href="{{ url_for("taco") }}">add a new taco</a>
              {% else %}
                  <a href="{{ url_for("login") }}">log in</a>
                  <a href="{{ url_for("register") }}">sign up</a>
              {% endif %}

          </nav>
          {% block content %}{% endblock %}
        </div>
      </div>
      <footer>
        <p>An MVP web app made with Flask on <a href="http://teamtreehouse.com">Treehouse</a>.</p>
      </footer>
    </div>
  </body>
</html>
templates/index.html
{% extends 'layout.html' %}

{% block content %}
<h2>Tacos</h2>
    {% if tacos.count() %}
        <table class="u-full-width">
          <thead>
            <tr>
              <th>Protein</th>
              <th>Cheese?</th>
              <th>Shell</th>
              <th>Extras</th>
            </tr>
          </thead>
          <tbody>
        {% for taco in tacos %}
           <tr>
             <td>{{ taco.protein }}</td>
             <td>{% if taco.cheese %} Yes {% else %} No {%
               endif %} </td>
             <td>{{ taco.shell }}</td>
             <td>{{ taco.extras }}</td>
            </tr>
        {% endfor %}
          </tbody>
        </table>
    {% else %}
        no tacos yet, sorry! please bear with us, 
                we should be having them shortly!
    {% endif %}
{% endblock %}